Mir-Ismaili
Mir-Ismaili

Reputation: 17008

npm run: Executes another (incorrect) script

This is scripts section of my package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "compile-prebuild": "tsc -p prebuild-tsconfig.json --pretty",
    "prebuild": "ts-node --project PreBuild/tsconfig.json PreBuild/prebuild.ts",

    "testJs": "node test.js",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "extract-i18n": "ng xi18n Paradise --i18n-format=xlf2 --output-path=i18n --i18n-locale=en && ng run Paradise:xliffmerge"
},

The wonderful thing is when I try npm run build or npm run build -- --prod another script (prebuild) is executed:

> npm run build -- --prod

> [email protected] prebuild ...
> ts-node --project PreBuild/tsconfig.json PreBuild/prebuild.ts

Now, if I rename prebuild script to pre-build (in package.json), everything is going to be alright:

> npm run build -- --prod

> [email protected] build ...
> ng build "--prod"
...

Now, if I reverted to back, the problem appears again!


> npm -v
6.7.0

Upvotes: 1

Views: 290

Answers (2)

Timothy Jones
Timothy Jones

Reputation: 22135

This is "correct", as it is the documented behaviour of npm - see here.

Additionally, arbitrary scripts can be executed by running npm run-script <stage>. Pre and post commands with matching names will be run for those as well (e.g. premyscript, myscript, postmyscript).

Generally, scripts can be prefixed with pre or post to do things before or after a script.

It is best to consider the prefixes pre and post as reserved when choosing npm script names (unless you intend them to always be run before or after the main task, of course).

Upvotes: 1

himank
himank

Reputation: 439

The pre and post hooks run automatically by npm. If you have prebuild defined in your package.json, npm will run it automatically when you ask it to run build. Same goes for post hook as well.

You can check out the documentation here. https://docs.npmjs.com/misc/scripts

Upvotes: 1

Related Questions