How to make a npm command, that executes two comma

2019-07-14 19:04发布

问题:

How can I make a command in the package.json file, that executes two commands for the terminal (console) one after another?


This question refers to node.js and NPM package.

I have a "server" folder in which I installed http server and npm package.json file, and there is also a folder "webdir" (inside "server" folder) with a web page.

I can run the web server manually, or by typing in the terminal: node [path to the file that runs the server], but I need this to happen automatically on command npm start. And the server must be activated from the "webdir" folder, so that I could immediately go to my web page at localhost: 8080.

To do this, I need to write in the terminal (console) two commands one after another:

  1. cd webdir
  2. node [path to the file that runs the Web server]

How to make a npm command, that executes two commands in the console (one after another)?

Here is the working code with a single line script:

"scripts": {
    "start": "node (...)/node_modules/http-server/bin/http-server"
  },

回答1:

The syntax I'm aware of involves using && to separate the commands, i.e.

"cd webdir && node ..."

This should work both on Windows and on Unix based systems.

For example, jQuery uses such combined statements in its deploy scripts:

"build": "npm install && grunt",
"start": "grunt watch",
"test": "grunt && grunt test:slow",
"precommit": "grunt lint:newer",
"commitmsg": "node node_modules/commitplease"

See https://github.com/jquery/jquery/blob/master/package.json



回答2:

Try a semi-colon after your first CLI statement, then your second statement after the semi-colon.