当我运行时:
npm install my-app
应用安装到 node_modules/my-app/...
我也试过
npm install -g my-app
但这也不起作用。
如何将模块直接安装到当前目录?
最佳答案
npm install
安装包locally or globally :
- 本地:
npm
在当前目录中查找名为node_modules
的现有文件夹,并为您在该文件夹中安装的每个包创建一个文件夹.如果它在此处找不到现有的node_modules
文件夹,则会查看当前目录的祖先,直到找到。如果找不到,则会在当前目录中创建一个。 - 全局:如果您使用
-g
(全局)选项,则软件包安装在全局位置。这个位置因 Linux 发行版而异,但/usr/local/lib/node_modules/packagename
就是一个例子。 CentOS7 使用/usr/lib/node_modules/packagename
。
只有当包是你想用作命令的东西时,你才应该使用 -g
。
Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
In general, the rule of thumb is:
- If you’re installing something that you want to use in your program, using
require('whatever')
, then install it locally, at the root of your project.- If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your
PATH
environment variable.
npm 不会将包的文件直接安装到当前目录中。
不过,这其实是件好事。它将依赖项的文件与您的应用程序分开,并且 Node automatically searches the node_modules
folder when you require
something .
关于node.js - npm 在当前目录安装模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032160/