Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding npm and Node.js install location for modules

Tags:

node.js

npm

I've been using Node.js and npm for a few weeks with great success and have started to question the best practice for installing local modules. I understand the Global vs Local argument, however, my question has more to do with where to place a local install. Let's say that I have a project located at ~/ProjectA/ which is version controlled and worked on by multiple developers. When initially playing with Node.js and npm I wasn't aware of the default local installation paths and just simply installed the necessary modules in a default terminal which resulted in a installation path of ~/node_modules. What this ended up doing is requiring all the other developers working on the project to install the modules on their own machines in order to run the application. Having seen where some of the developers ran npm install I'm still really surprised that it worked on their machines at all (I guess it relates to how Node.js and require() looks for modules), but needless to say, it worked.

Now that the project is getting past the "toying around" stage, I would like to setup the project folder correctly. So, my question is, should the modules be installed at ~/ProjectA/node_modules and therefore be part of the version controlled project files, or should it continue to be located at a developer-machine specific location...or does it not really matter at all?

I'm just looking for a little "best-practice" guidance on this one and what others do when setting up your projects.

like image 901
Sanuden Avatar asked Jun 03 '13 13:06

Sanuden


People also ask

Where should npm modules be installed?

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

Where does npm install node?

npm can install packages in local or global mode. In local mode, it installs the package in a node_modules folder in your parent working directory. This location is owned by the current user.

Does npm install node modules?

npm install doesn't create node_modules directory.


1 Answers

I think that the "best practice" here is to keep the dependencies within the project folder.
Almostly all Node projects I've seen so far (I'm a Node developer has about 8 months now) do that.

You don't need to version control the dependencies. That's how I manage my Node projects:

  1. Keep the versions locked in the package.json file, so everyone gets the same working version, or use the npm shrinkwrap command in your project root.
  2. Add the node_modules folder to your VCS ignore file (I use git, so mine is .gitignore)
  3. Be happy, you're done!
like image 116
gustavohenke Avatar answered Oct 01 '22 19:10

gustavohenke