Nickadiemus
Nickadiemus

Reputation: 411

Can Node.js pass down node_modules dependencies?

So I have a ec2 instance running on a free tier on amazon's web services. I'm running a Ubuntu server that has been configured to run at its public address. Here is the server.js code

const express = require('express')
const app = express()

/*
app.get('/', (req, res) => {
    res.send("CONTENT COMING")
})
*/

app.use(express.static('public'))
app.listen(3000, () => {console.log("Server Running on port 3000")})

as you can see I use express to serve a static file server just so I can develop locally and easily deploy online to the server when the time comes. Inside the public folder I have an index.html and a test.js file to serve. My question is will I have to create another instance of a node package manager to install packages to use in my program?

For example, if I want to require node.js File-System by using

const fs = require('fs')

would I have to create another npm init to then install my dependencies or could I somehow use my dependencies from the directory above?

Upvotes: 0

Views: 133

Answers (3)

You just need to insert the dependencies in your current project in your package.json and make sure that you create a deployment flow that installs it.

If you are having trouble to deploy to your EC2 Instance, I would recommend you to start one instance using Amazon EB and use CodeShip to deploy it.

Another option would be to run it using Heroku and integrating with git.

Both will take care of installing the dependencies before of running it.

Upvotes: 1

IzzyCooper
IzzyCooper

Reputation: 596

The answer is no. You only need one.

NPM modules can be incorporated into your code several folders down in your project.

Take for example a project folder:

/root
   app.js
   package.json
   /public
      index.html
   /node_modules
      ...

All modules in the node-module folder can be used in any project sub-folder.

Say, you have a file named anotherApp.js in a folder down the line, it will 'inherit' all node modules installed in any parent folder. Therefore, it will also be able to access every module that is in the project root node_module folder.

/root
   /somefoldername
      /anotherfolder
         /application
            anotherApp.js
   /node_modules
      ...

I should add, that NPM (Node package manager) is there to handle locally installed modules. The fs (File System) modules does come with node and is installed as a global, therefore you do not need to install it via NPM. Check out https://www.npmjs.com/ for a full list of available downloadable modules.

Upvotes: 2

James Maa
James Maa

Reputation: 1794

I would always suggest using git to synchronize your local files and your files on the EC2 server.

To install the same dependency on both your local and cloud, you can save it in your package.json e.g. npm install --save express

And on your server, npm install would install all the dependencies listed in package.json

Upvotes: 1

Related Questions