Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to add the 'run' argument when building an angular app (npm run build)?

I'm just being curious. This is a portion of the package.json file

"start": "ng serve -o",
"build": "ng build",
"build-prod": "ng build --prod",
"test": "ng test",

I'm just curious. Why does writing npm start and npm test work. However, npm build or npm build-prod gives me the following error build npm build called with no arguments. Did you mean to 'npm run-script build'? Why do I need to add run when building? npm run build or npm run build-prod

Thanks for helping

like image 966
Richard77 Avatar asked Oct 18 '25 02:10

Richard77


1 Answers

In the "Description" section of the npm-run documentation it states:

"run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well."

Note: Bold emphasis added by me

Essentially, npm considers any script defined in the scripts section of package.json that is named; test, start, restart, or stop, to be a "lifecycle" script. Any one of those four "lifecycle" scripts can be invoked without the run keyword.

Each of the aforementioned four npm script names, (i.e. "lifecycle" scripts) , have a decicated ClI Command, namely npm test, npm start, npm restart, and npm stop, for invoking them.

As an example consider the following contrived "scripts" section defined in package.json:

"scripts": {
  "start": "echo \"starting...\"",
  "restart": "echo \"restarting...\"",
  "stop": "echo \"stopping...\"",
  "test": "echo \"testing...\"",

  "foo": "echo \"foo...\""
},
  • The script named foo is the only script that must be invoked using the run keyword, i.e. npm run foo.

  • Unlike the others, namely: start, restart, stop, and test, they can be invoked without the run keyword; i.e. npm start, npm stop and so on.

    Side Note: start, restart, stop, and test can be invoked with the run keyword too. i.e. npm run start, npm run stop and so on.

The docs also state the following about npm run:

"If no "command" is provided, it will list the available scripts [...] When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts."

So given the package.json shown in your question, if you cd to your project directory and run npm run you'll see that both the start and test scripts are listed under the "Lifecycle" section.

like image 128
RobC Avatar answered Oct 20 '25 13:10

RobC