ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


i
ireddick

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

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)
a
aesede

In my case I need to do

sudo npm install  

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


sudo may be not required for npm install.
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.
never run npm with sudo unless you're using -g -- and even then there are better ways to handle that.
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!
@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.
H
H.A.

Just execute

sudo npm i --save

That's all