332

I have a web app: fooapp. I have a package.json in the root. I want to install all the dependencies in a specific node_modules directory. How do I do this?

What I want

Lets say I have two widget dependencies. I want to end up with a directory structure like this:

node_modules/
  widgetA
  widgetB
fooapp/
  package.js
  lib
  ..

What I get

when I run npm install fooapp/ I get this:

node_modules/
  fooapp/
    node_modules/
      widgetA
      widgetB
    package.js
    lib/
    ..
fooapp/
  package.js
  lib/
  ..

npm makes a copy of my app directory in the node_modules dir and installs the packages inside another node_modules directory.

I understand this makes sense for installing a package. But I don't require() my web app inside of something else, I run it directly. I'm looking for a simple way to install my dependencies into a specific node_modules directory.

4 Answers 4

618

Running:

npm install

from inside your app directory (i.e. where package.json is located) will install the dependencies for your app, rather than install it as a module, as described here. These will be placed in ./node_modules relative to your package.json file (it's actually slightly more complex than this, so check the npm docs here).

You are free to move the node_modules dir to the parent dir of your app if you want, because node's 'require' mechanism understands this. However, if you want to update your app's dependencies with install/update, npm will not see the relocated 'node_modules' and will instead create a new dir, again relative to package.json.

To prevent this, just create a symlink to the relocated node_modules from your app dir:

ln -s ../node_modules node_modules
1
  • 13
    Despite that it is a working solution, I found out npm link is more convenient in such situations where you need to grab modules from another directory (see stackoverflow.com/a/14387210/820520)
    – melekes
    Commented Jul 17, 2014 at 10:55
19

In my case I need to do

sudo npm install  

my project is inside /var/www so I also need to set proper permissions.

7
  • 29
    sudo may be not required for npm install.
    – Allen Koo
    Commented Dec 14, 2013 at 8:40
  • 2
    Depending on the user that needs to execute the js file and dependencies installed, you can run sudo -u [username] npm install. Better to just npm install and chown the node_modules directory recursively after the fact. Commented Jul 4, 2014 at 21:31
  • 6
    never run npm with sudo unless you're using -g -- and even then there are better ways to handle that.
    – tkone
    Commented Feb 3, 2015 at 15:53
  • 1
    Thanks for you answer, but why is that? can you explain? I mean, what if I have a project in which I want or need to keep a folder with root permissions and install dependencies without -g there? maybe there's a good reason, but I did sudo npm install (without -g) a few times and never got any trouble. Cheers!
    – aesede
    Commented Feb 3, 2015 at 17:55
  • 2
    @RredCat I agree about the right way of doing this kind of stuff, but after a while I noticed that this discussion is migrating towards Ubuntu, it seems there's a problem in the way it manages some permissions and folders, that is forcing users to use sudo npm. Again, only in Ubuntu.
    – aesede
    Commented Dec 7, 2015 at 18:22
1

Just execute

sudo npm i --save

That's all

0
npm i --force

from documention:

The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.