liveBook · Manning

Npm

As explained in Essential TypeScript 5, Third Edition

npm is a package manager for installing and managing JavaScript packages and their dependencies.

npm (Node Package Manager) installs the packages a TypeScript project needs, including the compiler, development tools, and application libraries. It puts locally installed packages in node_modules and follows each package’s dependencies to download compatible versions. Install npm alongside Node.js; check the installation with npm --version. section 5.3 section 2.1.1

A typical project setup separates development tools from application packages:

  • Run npm init --yes in the project folder to create package.json, which tracks added packages. section 14.1
  • Run npm install --save-dev typescript@5.0.2 to install the compiler locally and record it under devDependencies. Installing without --save-dev records an application package under dependencies. section 16.1 section 5.3
  • Run npm install to install the packages specified in package.json. A global install instead makes a package available outside one project; a local install keeps it project-specific. section 5.3

Set up compiler tools

In a project folder, run npm init --yes, then npm install --save-dev typescript@5.0.2 and npm install --save-dev tsc-watch@6.0.0. The resulting package.json records both tools in devDependencies. A start script can invoke tsc-watch and run the compiled JavaScript with Node.js; npm start runs that script. section 14.1 section 5.3

Listing 14.4 Configuring NPM in the package.json file in the decorators folder
{
  "name": "decorators",
  "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"
  },
  "type": "module"
}

The package.json listing shows the start script, local development dependencies, and module-format setting together. section 14.1

Use a local TypeScript installation when building a project toolchain rather than relying on a globally installed compiler. Package versions can be specified exactly or with an accepted range; the version recorded in package.json affects what npm may install. section 16.1 section 5.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains dependency resolution, install scope, package.json records, version specifications, and commands; does not descend into npm implementation internals.
Examples
Uses npm init, versioned installs of TypeScript and tsc-watch, a package.json start script, and npm start.
What it assumes
Assumes Node.js is installed while showing how to verify npm and create a package.json from scratch.
What you can do after
Operate/configure it to initialize a project, install local development tools, distinguish dependency groups, and start a configured compiler script.
How it connects
Connects npm to Node.js, the TypeScript compiler, package.json, node_modules, development dependencies, and compiler scripts.

Also explained in

Compare how 5 books explain npm →

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.