Package Management
| Command | Description |
npm init -y | Create package.json |
npm install <pkg> | Install dependency |
npm install -D <pkg> | Install dev dependency |
npm install -g <pkg> | Install globally |
npm uninstall <pkg> | Remove package |
npm update | Update all packages |
npm outdated | Check for outdated packages |
npm ls --depth=0 | List installed packages |
Scripts & Running
// package.json
{
"scripts": {
"dev": "node --watch server.js",
"build": "tsc",
"test": "jest",
"lint": "eslint .",
"start": "node dist/server.js"
}
}
// Run scripts
npm run dev
npm test // Shorthand (also: npm start, npm stop)
npm run build
npx — Run Without Installing
npx create-react-app my-app
npx ts-node script.ts
npx eslint --init
Security
npm audit # Check for vulnerabilities
npm audit fix # Auto-fix vulnerabilities
npm audit fix --force # Force fix (may break things)
Publishing
npm login
npm publish
npm publish --access public # For scoped packages
npm version patch|minor|major