Using npm install
installs the module into the current directory only (in a subdirectory called node_modules
).
(使用npm install
将模块安装到当前目录中(在名为node_modules
的子目录中)。)
Is app.js located under home/dave/src/server/
? (app.js是否位于home/dave/src/server/
?)
If not and you want to use the module from any directory, you need to install it globally using npm install -g
. (如果不是,并且您想从任何目录使用该模块,则需要使用npm install -g
全局npm install -g
。)
I usually install most packages locally so that they get checked in along with my project code.
(我通常在本地安装大多数软件包,以便它们与我的项目代码一起签入。)
Update (8/2019):
(更新(8/2019):)
Nowadays you can use package-lock.json file, which is automatically generated when npm modifies your node_modules directory.
(如今,您可以使用package-lock.json文件,该文件在npm修改您的node_modules目录时自动生成。)
Therefore you can leave out checking in packages, because the package-lock.json
tracks the exact versions of your node_modules, you're currently using. (因此,您package-lock.json
签入包,因为package-lock.json
跟踪您当前正在使用package-lock.json
的确切版本。)
To install packages from package-lock.json
instead of package.json
use the command npm ci
. (要从package-lock.json
而不是package.json
安装软件包,请使用命令npm ci
。)
Update (3/2016):
(更新(3/2016):)
I've received a lot of flak for my response, specifically that I check in the packages that my code depends on.
(我收到了很多回应,特别是我签入了我的代码所依赖的软件包。)
A few days ago, somebody unpublished all of their packages ( https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp ) which broke React, Babel, and just about everything else. (几天前,有人取消发布了所有软件包( https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp ),这破坏了React,Babel和几乎所有东西其他。)
Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you. (希望现在很清楚,如果您具有生产代码,则不能依赖NPM来真正为您维护依赖项。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…