npm install --production 忽略我的依赖

npm install --production ignore my dependencies

我有这个package.json:

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "dependencies": {
        "cross-env": "^7.0.3"
    },
    "devDependencies": {
        "autoprefixer": "^10.2.6",
        "axios": "^0.19",
        "browser-sync": "^2.26.14",
        "browser-sync-webpack-plugin": "^2.3.0",
        "cross-env": "^7.0.3",
        "laravel-mix": "^6.0.19",
        "less": "^3.13.1",
        "less-loader": "^7.3.0",
        "lodash": "^4.17.21",
        "resolve-url-loader": "^3.1.3",
        "vue-template-compiler": "^2.6.14"
    }
}

只是为了测试 NodeJS、NPM、Laravel 和 Laravel 混合 (Webpack) 是如何工作的,我 运行:

npm install --production (https://docs.npmjs.com/cli/v7/commands/npm-install)

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.

实际行为

当我转到 node_modules 文件夹时,我在那里没有看到任何 cross-env 包。此外命令行的输出是:

$ npm install --production

up to date, audited 1 package in 17s

found 0 vulnerabilities

,表示没有安装包(特别是:cross-env)。

预期行为

cross-env(并且仅 cross-env)将被安装,作为 dependencies 的一部分,使用此命令:npm run install --production.

问题

为什么 dependencies 数组似乎被 NPM 忽略了?

您有 cross-env 也列在 devDependencies 中。依赖关系只能在一个位置声明。也许 npm 忽略了 cross-env,因为它在 devDependencies.

中列出

我发现即使依赖项没有在 package.json 中明确列出两次,而是在 dependencies 中声明的包的子依赖项,也会发生这种情况。我的例子:

{
  "dependencies": {
    "sharp": "^0.26.1" // depends on "color" which depends on "color-convert"
  },
  "devDependencies": {
    "color-convert": "^2.0.1" 
  }
}

在此示例中,如果 运行 npm install --only=production,则不会安装 color-convert,即使它是生产依赖项的依赖项(使用 npm v6.14.14)