302

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79

4 Answers4

575

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
ireddick
  • 8,008
  • 2
  • 23
  • 21
  • 12
    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 http://stackoverflow.com/a/14387210/820520) – melekes Jul 17 '14 at 10:55
20

In my case I need to do

sudo npm install  

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

aesede
  • 5,541
  • 2
  • 35
  • 33
  • 26
    sudo may be not required for npm install. – Allen Koo Dec 14 '13 at 08: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. – TheLonelyGhost Jul 04 '14 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 Feb 03 '15 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 Feb 03 '15 at 17:55
  • @tkone, after a while I've come to this as a nice solution for Linux/OSX users that want to install globally without sudo: [npm install -g without sudo by Kazé](http://kazhack.org/?post/2014/12/11/npm-install-g-without-sudo). Hope it helps to (L)ubuntu users like me. – aesede Oct 22 '15 at 15:39
  • I don't think that is a good idea to use sudo with any command on console except the commands that really require the root permissions. – RredCat Dec 07 '15 at 13:46
  • 1
    @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 Dec 07 '15 at 18:22
2

Just execute

sudo npm i --save

That's all

H.A.
  • 350
  • 5
  • 14
1
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

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33
codex
  • 11
  • 3