liveBook · Manning

Npm script

As explained in Essential TypeScript 5, Third Edition

The scripts section is the package.json configuration section that defines named commands runnable through npm.

An npm script is a named command in the scripts section of package.json. It replaces long, easy-to-forget command lines with a short npm command such as npm start. section 5.5.2

Starting a TypeScript watcher

A tools/package.json file can define a start script that runs tsc-watch and executes the compiled program after successful compilation. Run it from the tools folder with npm start. section 5.5.2

Listing 5.17 Adding to the scripts section of the package.json file in the tools folder
{
  "name": "tools",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "tsc-watch --onsuccess \"node dist/index.js\""
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tsc-watch": "^6.0.0",
    "typescript": "^5.0.2"
  }
}

Inspect a complete scripts configuration that combines tsc-watch with node dist/index.js; use b10 for the command that invokes it. section 5.5.2

Scripts also provide a single entry point for development tools:

  • Define separate commands such as json for json-server and wds for webpack serve. section 17.1
  • Combine commands with npm-run-all so one npm start launches the web service and development server together. section 17.1
  • Expose common application tasks such as build, test, lint, and e2e alongside start. section 18.1.1
Listing 17.3 Configuring the development tools in the package.json file in the webapp folder
...
  "scripts": {
    "json": "json-server data.js -p 4600",
    "wds": "webpack serve",
    "start": "npm-run-all -p json wds"
  },
...

Compare a package.json configuration that starts json-server and webpack in parallel, then invoke it with npm start using b14. section 17.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines scripts through complete package.json examples and shows parallel execution, but does not detail npm script resolution internals.
Examples
Demonstrates tsc-watch, node dist/index.js, json-server, webpack serve, Angular CLI, and React scripts in package.json.
What you can do after
Operate development workflows with npm start and configure scripts that launch tools separately or in parallel.
How it connects
Connects package.json with TypeScript, webpack, Angular, React, json-server, npm-run-all, and development servers.

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 Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.