Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write multiline scripts in npm scripts?

Tags:

javascript

npm

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 ?

like image 510
user2331095 Avatar asked Mar 28 '16 08:03

user2331095


People also ask

Can the npm run all CLI tool run multiple npm scripts in parallel?

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 & .

How do I run multiple commands in node JS?

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.


1 Answers

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/

like image 115
asa Avatar answered Sep 18 '22 15:09

asa