javascriptnpm

How can I write multiline scripts in npm scripts?


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 ?


Solution

  • You can't do that.

    The following code is in read-json.js which is in package node_modules/npm/node_modules/read-package-json which is used in run-script.js to execute $ npm run-script ~~ or $ npm run ~~ which is its alias.

    function scriptpath (file, data, cb) {
      if (!data.scripts) return cb(null, data)
      var k = Object.keys(data.scripts)
      k.forEach(scriptpath_, data.scripts)
      cb(null, data)
    }
    
    function scriptpath_ (key) {
      var s = this[key]
      // This is never allowed, and only causes problems
      if (typeof s !== 'string') return delete this[key]
    
      var spre = /^(\.[\/\\])?node_modules[\/\\].bin[\\\/]/
      if (s.match(spre)) {
        this[key] = this[key].replace(spre, '')
      }
    }
    

    The key in scriptpath_ is like "build:server" in your code.

    The this[key] is like "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" in your code.

    So, if you write the code which is not string type, in other words, if you don't write the string text in package.json, it will be an error unless you contribute to the package npm/read-package-json.