如何安装软件包。使用npm在当前目录中的json依赖项

时间:2023-01-19 18:24:32

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?

我有一个网页应用程序:fooapp。我有一个包。json在根。我想在一个特定的node_modules目录中安装所有依赖项。我该怎么做呢?

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:

当我运行npm安装页脚/我得到这个:

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.

npm在node_modules目录中复制我的应用程序目录,并在另一个node_modules目录中安装包。

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.

我知道这对于安装一个包是有意义的。但是我不需要()我的web应用在别的东西里面,我直接运行它。我正在寻找一种简单的方法将依赖项安装到特定的node_modules目录中。

2 个解决方案

#1


298  

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).

从你的应用程序目录(例如哪里包)。将为您的应用程序安装依赖项,而不是像这里描述的那样将其作为模块安装。这些将放在./node_modules中,相对于您的包。json文件(它实际上比这个稍微复杂一些,请查看这里的npm文档)。

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.

如果您愿意,可以将node_modules目录移动到应用程序的父目录中,因为node的“require”机制理解这一点。但是,如果您想用install/update更新应用程序的依赖项,npm将不会看到重新定位的“node_modules”,而是会创建一个新的dir,同样是相对于package.json。

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

要避免这种情况,只需从app dir中创建一个指向重新定位的node_modules的符号链接:

ln -s ../node_modules node_modules

#2


12  

In my case I need to do

就我而言,我需要这么做

sudo npm install  

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

我的项目在/var/www中,所以我也需要设置适当的权限。

#1


298  

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).

从你的应用程序目录(例如哪里包)。将为您的应用程序安装依赖项,而不是像这里描述的那样将其作为模块安装。这些将放在./node_modules中,相对于您的包。json文件(它实际上比这个稍微复杂一些,请查看这里的npm文档)。

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.

如果您愿意,可以将node_modules目录移动到应用程序的父目录中,因为node的“require”机制理解这一点。但是,如果您想用install/update更新应用程序的依赖项,npm将不会看到重新定位的“node_modules”,而是会创建一个新的dir,同样是相对于package.json。

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

要避免这种情况,只需从app dir中创建一个指向重新定位的node_modules的符号链接:

ln -s ../node_modules node_modules

#2


12  

In my case I need to do

就我而言,我需要这么做

sudo npm install  

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

我的项目在/var/www中,所以我也需要设置适当的权限。