liveBook · Manning

Package.json

As explained in Web Components in Action

package.json is a JSON configuration file that describes a JavaScript package, including its metadata, dependencies, and executable scripts.

package.json is a JSON project record. Create it with npm init to capture package details, track installed dependencies, separate development-only tools, and define commands for repeatable tasks. section 12.3

Use its main areas to organize a JavaScript project:

  • dependencies records packages needed by the project, such as Web Component polyfills. section 12.3
  • devDependencies records tools used only during development or building, such as Rollup installed with --save-dev. section 12.3 section 12.4
  • scripts maps names such as test or build to terminal commands, including commands that combine several component builds. section 12.3 section 12.4

A minimal script

A package.json can define a test command directly: { "name": "wcia", "version": "1.0.0", "scripts": { "test": "echo 'Hello from package.json'" } }. Run it through npm rather than typing the command manually. section 12.3

Listing 12.7. A simple package.json script
{
 "name": "wcia",
 "version": "1.0.0",
 "scripts": {
   "test": "echo 'Hello from package.json'"       #1
 }
}

Inspect a complete minimal package.json script and its test command. section 12.3

Listing 12.8. A package.json file after installing Rollup
{
 "name": "wcia",
 "version": "1.0.0",
 "dependencies": {},
 "devDependencies": {
   "rollup": "^1.0.2"         #1
 }
}

Compare dependencies with a Rollup devDependencies entry after local installation. section 12.4

Listing 12.9. Adding a Rollup script in package.json
{
 "name": "wcia",
 "version": "1.0.0",
 "dependencies": {},
 "devDependencies": {
   "rollup": "^1.0.2"
 },
 "scripts": {
 "build": "./node_modules/.bin/rollup chapter12and13/components/slider/src/#1
   slider.js --file chapter12and13/components/slider/slider.js --format    #1
   umd --name slider -m"                                                   #1
}

Follow a package.json build script that invokes a locally installed Rollup binary. section 12.4

How Web Components in Action covers it

How deep it goes
Defines creation, dependency categories, and scripts through examples, but does not detail the full JSON schema.
Examples
Uses npm init, Web Component polyfill installation, an echo test script, and Rollup build scripts for components.
What it assumes
Assumes basic terminal use and introduces npm as the Node.js package-installation tool before building workflows.
What you can do after
Operate/configure npm to record packages and run development or Rollup build commands through named scripts.
How it connects
Connects npm, Node.js, dependencies, devDependencies, scripts, Rollup, module bundling, and build processes.

Also explained in

Compare how 5 books explain package.json →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.