且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 npm 在当前目录中安装 package.json 依赖项

更新时间:2022-06-12 22:20:46

运行:

npm install

从您的应用程序目录中(即 package.json 所在的位置)为您的应用程序安装依赖项,而不是将其安装为模块,如此处所述.这些将放置在相对于您的 package.json 文件的 ./node_modules 中(它实际上比这稍微复杂一些,因此请查看 npm 文档在这里).

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 目录移动到应用程序的父目录,因为节点的要求"机制理解这一点.但是,如果您想通过安装/更新来更新应用的依赖项,npm 将不会看到重新定位的node_modules",而是会创建一个新目录,同样是相对于 package.json.

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 的符号链接:

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

ln -s ../node_modules node_modules