运行npm install
我收到了这些警告:
npm WARN The package babel-core is included as both a dev and production dependency.
npm WARN The package babel-loader is included as both a dev and production dependency.
npm WARN The package babel-preset-react is included as both a dev and production dependency.
npm WARN The package redux-thunk is included as both a dev and production dependency.
npm WARN The package uglifyjs is included as both a dev and production dependency.
up to date in 7.183s
npm WARN The package babel-core is included as both a dev and production dependency.
npm WARN The package babel-loader is included as both a dev and production dependency.
npm WARN The package babel-preset-react is included as both a dev and production dependency.
npm WARN The package redux-thunk is included as both a dev and production dependency.
npm WARN The package uglifyjs is included as both a dev and production dependency.
package.json
档案:
{
...
"dependencies": {
"axios": "^0.15.3",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.9.0",
"bluebird": "^3.5.0",
"bootstrap-sass": "^3.3.7",
"classnames": "^2.2.5",
"console-polyfill": "^0.2.3",
"cross-env": "^1.0.8",
"css-loader": "^0.23.1",
"deepmerge": "^1.3.2",
"dom-helpers": "^3.0.0",
"expect": "^1.20.1",
"fuzzy": "^0.1.3",
"moment": "^2.18.1",
"node-libs-browser": "^1.0.0",
"node-sass": "^3.8.0",
"react": "^15.1.0",
"react-addons-shallow-compare": "15.4.0",
"react-addons-test-utils": "^15.1.0",
"react-axios": "0.0.9",
"react-bootstrap-daterangepicker": "^3.2.2",
"react-daterange-picker": "^1.1.0",
"react-dom": "^15.1.0",
"react-draggable": "^2.2.3",
"react-redux": "^4.4.8",
"react-router": "^3.0.2",
"react-virtualized": "^8.5.2",
"redux": "^3.6.0",
"redux-logger": "^2.6.1",
"redux-promise": "^0.5.3",
"redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.1.0",
"sass-loader": "^4.0.0",
"style-loader": "^0.13.1",
"uglifyjs": "=2.4.10",
"webpack-dev-middleware": "^1.6.1",
"webpack-dev-server": "^1.14.1",
"webpack-hot-middleware": "^2.11.0"
},
"devDependencies": {
"assets-webpack-plugin": "^3.5.1",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2016": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"didyoumean": "^1.2.1",
"extract-text-webpack-plugin": "^1.0.1",
"glob": "^7.1.1",
"postcss-loader": "^1.3.0",
"purifycss-webpack-plugin": "^2.0.3",
"react-transform-hmr": "^1.0.4",
"redux-thunk": "^2.2.0",
"uglifyjs": "=2.4.10",
"webpack": "^1.15.0",
"webpack-cleanup-plugin": "^0.5.1",
"webpack-split-chunks": "^0.1.1"
}
}
注意到安装了不同的版本。如果为dev和prod安装了相同软件包的两个版本,究竟会发生什么?这是正常的行为吗?我应该摆脱package.json文件中的欺骗引用吗?
答案 0 :(得分:20)
您拥有依赖项的两个部分中引用的包;您应该完全不这样做,因为这意味着您的生产安装将具有与开发安装不同的版本。
如果您执行npm install
,您将获得所有dependencies
&已安装devDependencies
;但是,如果您执行npm install --production
,则只能安装dependencies
。
您应该从dependencies
删除您的应用不需要的内容,并将其放在devDependencies
中。 dependencies
中的内容应视为运行应用程序的要求(在任何代码转换发生之后)。
零依赖的情况应该是零。
答案 1 :(得分:0)
我的用例正是@Jim在接受的答案的注释中指出的,在开发中,我想在使用它的同时使用其他模块的同时使用本地模块文件。 在生产中,我将使用VCS中的模块,并且我不想每次都手动更改package.json文件。
这是我设置package.json的方式:
"dependencies": {
"module-name": "git+ssh://git@bitbucket.org/XXX/XXX.git#master"
},
"devDependencies": {
"module-name-dev": "file:../XXX"
}
使用此设置,npm不会给我任何错误,因为模块名称不同,现在剩下要做的就是在开发中需要dev软件包,而不是主要软件包。
我找到了module-alias软件包,它使您可以将别名用于所需的路径。
在最开始的app.js文件中,您需要添加以下代码:
if (process.env.NODE_ENV === 'development') {
const moduleAlias = require('module-alias');
moduleAlias.addAlias('module-name', 'module-name-dev');
}
从现在开始,每当您需要module-name
模块时,您实际上就会开发module-name-dev
。
在生产环境中,您不应该安装devDependencies,并且别名将不起作用,因此不需要在2之间进行任何更改。
如果您使用的是webpack,则不需要module-alias
,可以使用webpack-chain这样的别名将别名添加到webpack配置中:
chainWebpack: (config) => {
if (process.env.NODE_ENV === 'development') {
config.resolve.alias
.set('module-name', 'module-name-dev');
}
},