My package.json looks like the following:
{ "name": "project", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "lint": "./node_modules/eslint/bin/eslint.js --format \"./node_modules/eslint-friendly-formatter/index.js\" .", "build:server": "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" } }
As you can see, the lint
and build:server
command are hard to read, so I want to break them into multiple lines.
I've tried to use \
, but it throws errors like:
npm ERR! Failed to parse json npm ERR! Unexpected token ' ' at 11:80 npm ERR! :server": "./node_modules/babel-cli/bin/babel.js . -d dist/server \ npm ERR! ^
How can I do this?
Only to write another bash file like build.sh
and use it in npm scripts like ./build.sh server
?
The official npm run-script command cannot run multiple scripts, so if we want to run multiple scripts, it's redundant a bit. Let's shorten it by glob-like patterns. Cross platform. We sometimes use & to run multiple command in parallel, but cmd.exe ( npm run-script uses it by default) does not support the & .
After running npm i concurrently to install it, you can then set up your NPM start script to run multiple commands just by separating each individual command with quotes. And once again, you should be off to the races.
You can chain independent tasks.
Here is an example:
"scripts": { "lint-jshint": "jshint --verbose --show-non-errors ./src/main/js", "lint-eslint": "eslint ./src/main/js ./src/test/js", "lint-csslint": "csslint ./src/main/js", "lint": "npm run -s lint-jshint & npm run -s lint-eslint & npm run -s lint-csslint", "pretest": "rimraf ./build/reports/tests && mkdirp ./build/reports/tests && npm run -s lint", "test": "karma start ./src/test/resources/conf/karma.conf.js", ...
Here is a nice blog which I used at that time: https://www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With