liveBook · Manning

Package

As explained in Essential TypeScript 5, Third Edition

A JavaScript package is a distributable collection of JavaScript code and metadata intended for reuse in software projects.

A JavaScript package supplies reusable code or development tools to a project. TypeScript projects use ordinary JavaScript packages because the TypeScript compiler produces JavaScript. A package may include type information; when it does not, a separate type declaration package may be available. section 2.6 section 15.0

NPM installs project packages into node_modules. It also follows each package’s declared dependencies and selects versions that satisfy their requirements. The project’s package.json records installed packages: dependencies are part of the application, while devDependencies are tools needed during development. section 5.3

{
 "name": "tools",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [],
 "author": "",
 "license": "ISC",
 "devDependencies": {
   "tsc-watch": "^6.0.0",
   "typescript": "^5.0.2"
 }
}

Inspect the package.json example: typescript and tsc-watch appear under devDependencies because they are development tools, not application dependencies. section 5.3

Install the compiler for a web app

In a new webapp folder, run `npm init --yes` to create package.json, then `npm install --save-dev typescript@5.0.2`. This toolchain needs a local TypeScript installation; a globally installed compiler is not a substitute for the project package. section 16.1

Choose the installation scope deliberately. A local package belongs to one project; a global installation makes a tool available from anywhere. Use `npm install --save-dev package@version` for a development tool, or omit `--save-dev` for an application dependency. Version requirements can specify an exact version or a permitted range. section 5.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains NPM installation, dependency resolution, version requirements, installation scope, and package.json categories without detailing package publication.
Examples
Uses a package.json containing typescript and tsc-watch, plus commands that install a local TypeScript compiler for a web app.
What it assumes
Assumes some JavaScript familiarity and flags JavaScript modules as background sometimes needed before installing a package in TypeScript.
What you can do after
Operate/configure packages with NPM, placing development tools and application dependencies in the appropriate package.json categories.
How it connects
Connects packages to NPM, package.json, node_modules, TypeScript compilation, and type declarations.

Also explained in

Compare how 8 books explain package →

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.