javascript Npm postinstall 仅用于开发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23076968/
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 postinstall only on development
提问by just-boris
I have npm module with following package.json
我有以下 package.json 的 npm 模块
{
"name": "my-app",
"version": "0.0.0",
"scripts": {
"prepublish": "bower install",
"build": "gulp"
},
"dependencies": {
"express": "~4.0.0",
"body-parser": "~1.0.1"
},
"devDependencies": {
"gulp": "~3.6.0",
"bower": "~1.3.2"
}
}
When I deploy my app to production I don't want install devDependecies so, I run npm install --production
. But in this case prepublish
script are called, but it doesn't need, because I use CDN links in production.
当我将我的应用程序部署到生产环境时,我不想安装 devDependcies 所以,我运行npm install --production
. 但在这种情况下,prepublish
脚本被调用,但它不需要,因为我在生产中使用 CDN 链接。
How to call postinstall script only after npm install
but not after npm install --production
?
如何仅在之后npm install
而不是之后调用安装后脚本npm install --production
?
采纳答案by mgol
Newer npm (& Yarn) versions include support for the prepare
script that is run after each install
run but only in development mode. Also, the prepublish
is deprecated. This should be enough:
较新的 npm (& Yarn) 版本包括对prepare
每次install
运行后运行但仅在开发模式下运行的脚本的支持。此外,prepublish
已弃用。这应该足够了:
{
scripts: {
"prepare": "bower install"
}
}
回答by Sam Mikes
I think you cannot choose what scripts are run based on the --production
argument. What you can do, however, is supply a script which tests the NODE_ENV
variable and only runs bower install
if it's not "production".
我认为您不能根据--production
参数选择运行哪些脚本。但是,您可以做的是提供一个脚本来测试NODE_ENV
变量并且仅在bower install
它不是“生产”时才运行。
If you are always in a unix-y environment, you can do it like this:
如果你总是在 unix-y 环境中,你可以这样做:
{
scripts: {
"prepublish": "[ \"$NODE_ENV\" != production ] && bower install"
}
}
回答by Synthe
This only works if you're on a unix-like environment:
这仅在您处于类 Unix 环境中时才有效:
NPM sets an environment variable to "true" when install is run with --production. To only run the postinstall script if npm install was not run with --production, use the following code.
当使用 --production 运行安装时,NPM 将环境变量设置为“true”。要仅在未使用 --production 运行 npm install 时运行 postinstall 脚本,请使用以下代码。
"postinstall": "if [ -z \"$npm_config_production\" ]; then node_modules/gulp/bin/gulp.js first-run; fi",
回答by Giovanni Bruno
I work with windows, osx and linux so I use a NON environment specific solution to solve this problem:
我使用 windows、osx 和 linux,所以我使用非环境特定的解决方案来解决这个问题:
In the postinstallhandler i execute a js script that checks process.env.NODE_ENV variable and does the work.
在安装后处理程序中,我执行一个 js 脚本来检查 process.env.NODE_ENV 变量并完成工作。
in my specific case I have to execute a gulp task only in development env:
在我的特定情况下,我必须仅在开发环境中执行 gulp 任务:
part of package.json
package.json 的一部分
"scripts": {
"postinstall": "node postinstall"
}
all postinstall.js script
所有 postinstall.js 脚本
if (process.env.NODE_ENV === 'development') {
const gulp = require('./gulpfile');
gulp.start('taskname');
}
last row of gulpfile.js
gulpfile.js 的最后一行
module.exports = gulp;
it's important to export gulp from the gulpfile.js because all tasks are in that specific gulp instance.
从 gulpfile.js 导出 gulp 很重要,因为所有任务都在那个特定的 gulp 实例中。
回答by Alexey Vishentsev
Solution that is less dependent on unix nature of your shell:
不太依赖于 shell 的 unix 特性的解决方案:
"scripts": {
"postinstall": "node -e \"process.env.NODE_ENV != 'production' && process.exit(1)\" || echo do dev stuff"
},
回答by Már ?rlygsson
I have a more general problem - where I want to skip running the postinstall
script on local (direct) installs - like when I'm developing the package and run yarn add --dev my-new-dependency
.
我有一个更普遍的问题 - 我想跳过postinstall
在本地(直接)安装上运行脚本 - 比如当我开发包并运行yarn add --dev my-new-dependency
.
This is what I came up with. It works with both npm and yarn.
这就是我想出的。它适用于 npm 和 yarn。
postinstall.js:
安装后.js:
const env = process.env;
if (
// if INIT_CWD (yarn/npm install invocation path) and PWD
// are the same, then local (dev) install/add is taking place
env.INIT_CWD === env.PWD ||
// local (dev) yarn install may have been run
// from a project subfolder
env.INIT_CWD.indexOf(env.PWD) === 0
) {
console.info('Skipping `postinstall` script on local installs');
}
else {
// do post-installation things
// ...
}
package.json:
包.json:
"script": {
"postinstall": "node postinstall.js",
...
回答by JacopKane
I'm using if-envmodule. It's less verbose.
我正在使用if-env模块。它不那么冗长。
PS: I didn't test it on windows yet.
PS:我还没有在windows上测试过。
Install with:
安装:
npm i if-env
than in package.json
scripts:
比package.json
脚本:
"postinstall-production": "echo \"production, skipping...\"",
"postinstall-dev": "echo \"doing dev exclusive stuff\"",
"postinstall": "if-env NODE_ENV=production && npm run postinstall-production || npm run postinstall-dev"
回答by Wouter Van Vliet
Landed here because I had the same issue. Ended up with a solution that tests for the existence of a package under node_modules that I know should only be available in development.
登陆这里是因为我遇到了同样的问题。最终得到了一个解决方案,该解决方案测试 node_modules 下的包是否存在,我知道它应该只在开发中可用。
{
"scripts": {
"postinstall": "bash -c '[ -d ./node_modules/@types ] && lerna run prepare || echo No postinstall without type info'"
}
}
This works fine for me conceptually, as the prepare scripts here called by lerna are mainly to ts-to-js compilations.
这在概念上对我来说很好用,因为这里被 lerna 调用的准备脚本主要用于 ts-to-js 编译。