Intro
Better NPM scripts runner
- Avoid hard-coded commands in package.json
- Cross-platform compatibility, originally needed by:
Inspired by
Alternatives
Usage in package.json
From this:
{
"scripts": {
"build:dist": "NODE_ENV=development webpack --config $npm_package_webpack --progress --colors",
"test": "NODE_ENV=production karma start"
}
}
To this:
{
"devDependencies": {
"better-npm-run": "~0.0.1"
},
"scripts": {
"build:dist": "better-npm-run build:dist",
"test": "better-npm-run test"
},
"betterScripts": {
"build:dist": {
"command": "webpack --config $npm_package_webpack --progress --colors",
"env": {
"NODE_ENV": "development"
}
},
"build:prod": {
"command": "webpack --config $npm_package_webpack --progress --colors",
"env": {
"NODE_ENV": "production"
}
},
"test": {
"command": "karma start",
"env": {
"NODE_ENV": "test"
}
}
}
}
.env File
If you have an .env
file in your project root it will be loaded on every command
NODE_PATH=./:./lib
PORT=5000
Shell scripts
Currently, using bash variables (PWD, USER, etc.) is not possible:
"command": "forever start -l ${PWD}/logs/forever.log -o ${PWD}/logs/out.log -e ${PWD}/logs/errors.log -a index.js",
In order to use them, you can create an script file (.sh
) instead:
forever.sh
:
forever start -l ${PWD}/logs/forever.log -o ${PWD}/logs/out.log -e ${PWD}/logs/errors.log -a index.js
package.json
:
"command": "./forever.sh"