liveBook · Manning

Npm script

As explained in Pro Angular 16

The scripts section is the package.json field containing named command definitions that a package manager can execute.

An npm script is a named command definition in the scripts field of package.json. It gives a project a consistent way to run development, build, watch, and test tools. section 9.2.2

...
"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "build": "ng build",
  "watch": "ng build --watch --configuration development",
  "test": "ng test"
},
...

A typical Angular package.json defines scripts that map short names to Angular CLI commands. section 9.2.2

Run a script with npm run followed by its name from the folder containing package.json. For example, npm run test executes the test definition; ng test produces the same result for this project. section 9.2.2

Common project scripts provide these operations:

Adding a server command

After installing json-server locally, add a json script such as "json": "json-server --p 3500 restData.js". Run it from the exampleApp folder with npm run json to serve the configured data file on port 3500. section 23.1

Listing 23.2. Adding a script entry in the package.json file in the exampleApp folder
...
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"json": "json-server --p 3500 restData.js"        
},
...

The complete package.json change shows how a project combines standard Angular commands with a custom json-server command. section 23.1

How Pro Angular 16 covers it

How deep it goes
Defines command mappings and execution syntax, but does not detail npm’s script resolution internals.
Examples
Uses start, build, watch, test, and json scripts, including a json-server command serving restData.js on port 3500.
What it assumes
Assumes familiarity with package.json and npm installation, while demonstrating script syntax and execution from the project directory.
What you can do after
Operate/configure project tools through named npm commands executed from the package.json directory.
How it connects
Connects package.json with npm, local packages, Angular CLI, development servers, production builds, and unit testing.

Also explained in

Compare how 4 books explain npm script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.