node.js npm install:有没有办法忽略 package.json 中的特定依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47374072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
npm install: Is there a way to ignore a particular dependency in package.json
提问by user1790300
I am currently trying to create a docker container for a node.js project that contains a local dependency. This seems to cause an issue with docker so as a workaround I am trying to just copy the local dependency folders and just ignore their dependency entries in the package.json file. Is there a way to specify dependencies I would like to ignore and have npm install run and skip those enties?
我目前正在尝试为包含本地依赖项的 node.js 项目创建一个 docker 容器。这似乎会导致 docker 出现问题,因此作为一种解决方法,我试图只复制本地依赖文件夹并忽略它们在 package.json 文件中的依赖项。有没有办法指定我想忽略的依赖项并运行 npm install 并跳过这些条目?
回答by RootHacker
That can be done using devDependencies
这可以使用devDependencies完成
The npm modules which you require only to develop, e.g.: unit tests, Coffeescript to Javascript transpilation, minification etc,make the required module a devDependency.
您只需要开发的 npm 模块,例如:单元测试、Coffeescript 到 Javascript 的转换、缩小等,使所需的模块成为 devDependency。
To skip Installation of devDepenencies pass --productionflag to npm install,with the --productionflag(or NODE_ENVenvironment variable set to production) npmwill not install modules listed in devDependencies."
要跳过安装 devDepenencies 将--production标志传递给npm install,--production标志(或NODE_ENV环境变量设置为production)npm将不会安装 devDependencies 中列出的模块。”
npm install --production
To make any module to be part of devDependencies pass --dev while installing.
要使任何模块成为 devDependencies 的一部分,请在安装时通过 --dev。
npm install packagename --save-dev
回答by Vadim
It is a common issue, not only with Docker, but also with some cloud deployments. For example deployment to CloudFoundry using standard Node.js buildpack will cause npm install/yarnto run anyway. So, you'll also need to apply some tricks to work with local modules
这是一个常见问题,不仅在 Docker 中,而且在一些云部署中也是如此。例如,使用标准 Node.js buildpack 部署到 CloudFoundry 将导致npm install/yarn无论如何运行。因此,您还需要应用一些技巧来处理本地模块
If you don't mind to switch from NPM to Yarn for dependency management, you can use workspacesfeature.
如果您不介意从 NPM 切换到 Yarn 进行依赖管理,您可以使用工作区功能。
My package.jsonlooks like this:
我的package.json看起来像这样:
{
...
"dependencies": {
"some-module-i-want-to-install": "1.0.0",
"another-module-i-want-to-install": "1.0.0",
"@my/local-dependency-one": "1.0.0",
"@my/local-dependency-two": "1.0.0"
},
"workspaces": ["packages/*"]
}
And my project source layout has the following structure:
我的项目源布局具有以下结构:
.
├── index.js
├── package.json
├── packages
│?? ├── local-dependency-one
│?? │?? ├── index.js
│?? │?? └── package.json
│?? └── local-dependency-two
│?? ?? ├── index.js
│?? ?? └── package.json
└── yarn.lock
After running yarn, modules I want to install are fetched from NPM registry, and local dependencies are installed from packagesdirectory to node_modules.
运行后yarn,我要安装的模块从NPM注册表中获取,本地依赖从packages目录安装到node_modules.
.
├── index.js
├── node_modules
│?? ├── @my
│?? │ ├── local-dependency-one
│?? │ │ └── ...
│?? │ └── local-dependency-two
│?? │ └── ...
│?? ├── another-module-i-want-to-install
│?? │ └── ...
│?? └── some-module-i-want-to-install
│?? └── ...
├── package.json
├── packages
│?? ├── local-dependency-one
│?? │?? └── ...
│?? └── local-dependency-two
│?? ?? └── ...
└── yarn.lock
As you can see, I prefer to define my local packages as scoped(@my/...). It is not mandatory, but a best practice. NPM treats scoped packages as private by default, so I don't need to worry that they will be occasionally published or explicitly mark them as private.
如您所见,我更喜欢将本地包定义为作用域( @my/...)。这不是强制性的,而是最佳实践。默认情况下,NPM 将作用域包视为私有的,因此我不必担心它们会偶尔发布或明确标记为私有。

