NPM

NPM Logo

See NPM package for details on creation and publishing with TypeScript, Rollup, Vitest, and Changesets

NPM Basics

Initialize NPM Project

npm init

Install Package

npm install <package-name>
npm i <package-name>

Install Package as Dev Dependency

npm install <package-name> --save-dev
npm i <package-name> -D

Uninstall Package

npm uninstall <package-name>

Show Outdated Packages

npm outdated

Update Package

Note: This will update to the latest version according to the versioning rules in package.json. To update to the absolute latest version, you may need to modify the version specifier in package.json first.

npm update <package-name>

Semantic versioning

SpecifierExampleWhat it Allows (Range)Updates toUse For
^ (Caret)^1.2.3>=1.2.3 <2.0.0Minor and PatchGeneral use, new features
~ (Tilde)~1.2.3>=1.2.3 <1.3.0Patch onlyStability-critical bug fixes
None1.2.31.2.3Exact version onlyMaximum stability, critical production

List Installed Packages

npm list

Show Global Packages

npm list -g --depth=0

Run Script from package.json

npm run <script-name>

Run script from bash

npx <script-name>

Audit for Vulnerabilities

npm audit

Fix Vulnerabilities

npm audit fix
npm audit fix --force

View Package Info

npm view <package-name>

Package.json Key Fields

Specs for package.json file used by NPM to manage project metadata and dependencies can be found at the NPM Package.json Docs.

You can create a package.json file by running npm init in your project directory.

Common Fields to Update

These fields are typically manually updated to reflect the project information for a general web project.

FieldTypeDescription
nameStringName of the package
versionSemVer StringVersion of the package
descriptionStringShort description of the package
licenseStringLicense type for the package
homepageStringURL of the project homepage
scriptsObject with Script keysScripts that can be run using npm run <name>
privateBooleanIf true, prevents publishing the package
repositoryObject {type, url}Repository URL and type
keywordsArray[] of StringsKeywords for package discovery
authorObject {name, email, url}Author of the package

Fields Managed with NPM commands

This fields are typically managed automatically by NPM commands like npm install, npm uninstall, and npm update

FieldTypeDescription
dependenciesObject keys package-name:semverRuntime dependencies
devDependenciesObject keys package-name:semverDevelopment dependencies

Example Web Project package.json

{
  "name": "name-app",
  "version": "0.0.7",
  "description": "name-app description",
  "license": "MIT",
  "homepage": "https://pennockprojects.com/projects/jamstart",
  "scripts": {
    "custom": "echo 'custom script running'"
  },
  "private": true,
  "repository": {
    "type": "git",
    "url": "git+https://github.com/PennockProjects/jamstart.git"
  },
  "author": {
    "name": "John Pennock",
    "email": "info@pennockprojects.com",
    "url": "https://pennockprojects.com"
  },
  "keywords": [
    "example",
    "npm",
    "package.json"
  ],
}

Footnotes