How do I install package.json dependencies in the current directory using npm

369,686

Solution 1

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

Solution 2

In my case I need to do

sudo npm install  

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

Solution 3

Just execute

sudo npm i --save

That's all

Share:
369,686

Related videos on Youtube

Daniel Beardsley
Author by

Daniel Beardsley

I'm an independent Web Developer located on the Central Coast of California. I mostly develop using Ruby on Rails, but have been trying out Sinatra, Node.js, and other fun things as of late.

Updated on June 07, 2020

Comments

  • Daniel Beardsley
    Daniel Beardsley almost 4 years

    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.

  • Allen Koo
    Allen Koo over 10 years
    sudo may be not required for npm install.
  • TheLonelyGhost
    TheLonelyGhost almost 10 years
    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.
  • melekes
    melekes almost 10 years
    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)
  • tkone
    tkone over 9 years
    never run npm with sudo unless you're using -g -- and even then there are better ways to handle that.
  • aesede
    aesede over 9 years
    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
    aesede over 8 years
    @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é. Hope it helps to (L)ubuntu users like me.
  • RredCat
    RredCat over 8 years
    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.
  • aesede
    aesede over 8 years
    @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.