简体   繁体   English

npm install:有没有办法忽略package.json中的特定依赖项

[英]npm install: Is there a way to ignore a particular dependency in package.json

I am currently trying to create a docker container for a node.js project that contains a local dependency. 我目前正在尝试为包含本地依赖项的node.js项目创建一个docker容器。 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. 这似乎导致docker的问题,所以作为一种解决方法我试图只复制本地依赖项文件夹,并忽略它们在package.json文件中的依赖项。 Is there a way to specify dependencies I would like to ignore and have npm install run and skip those enties? 有没有办法指定我想忽略的依赖项并让npm安装运行并跳过这些内容?

That can be done using devDependencies 这可以使用devDependencies完成

The npm modules which you require only to develop, eg: unit tests, Coffeescript to Javascript transpilation, minification etc,make the required module a devDependency. 您只需要开发的npm模块,例如:单元测试,Coffeescript到Javascript的转换,缩小等,使所需的模块成为devDependency。

To skip Installation of devDepenencies pass --production flag to npm install ,with the --production flag(or NODE_ENV environment variable set to production ) npm will 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

It is a common issue, not only with Docker, but also with some cloud deployments. 这是一个常见问题,不仅与Docker有关,还与一些云部署有关。 For example deployment to CloudFoundry using standard Node.js buildpack will cause npm install / yarn to run anyway. 例如,使用标准Node.js buildpack部署到CloudFoundry将导致npm install / yarn无论如何都要运行。 So, you'll also need to apply some tricks to work with local modules 因此,您还需要应用一些技巧来使用本地模块

If you don't mind to switch from NPM to Yarn for dependency management, you can use workspaces feature. 如果您不介意从NPM切换到Yarn以进行依赖关系管理,则可以使用工作区功能。

My package.json looks 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 packages directory 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/... ). 如您所见,我更喜欢将本地包定义为作用域@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. 默认情况下 ,NPM将范围包视为私有 ,因此我不必担心它们会偶尔发布或明确标记为私有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM