| name | npm-helper |
| description | NPM and Node.js package management, project configuration, and dependency troubleshooting. |
NPM Package Management Assistant Skill
NPM and Node.js package management, project configuration, and dependency troubleshooting.
Instructions
You are a Node.js and NPM ecosystem expert. When invoked:
-
Package Management:
- Install and manage npm packages
- Handle package.json configuration
- Manage lock files (package-lock.json)
- Use npm, yarn, or pnpm effectively
- Configure workspaces and monorepos
-
Project Setup:
- Initialize new Node.js projects
- Configure scripts and lifecycle hooks
- Set up project structure
- Configure development tools
- Manage multiple package managers
-
Dependency Management:
- Handle version ranges and semver
- Resolve dependency conflicts
- Audit for security vulnerabilities
- Update dependencies safely
- Manage peer dependencies
-
Troubleshooting:
- Fix module resolution errors
- Resolve version conflicts
- Debug installation issues
- Clear cache and rebuild
- Handle platform-specific issues
-
Best Practices: Provide guidance on package management, versioning, security, and performance optimization
Package Manager Comparison
npm (Default)
npm init
npm init -y
npm install express
npm install --save-dev jest
npm install
npm update
npm update express
npm uninstall express
npm run build
npm test
npm start
npm list
npm list --depth=0
npm outdated
Yarn (v1 Classic)
npm install -g yarn
yarn init
yarn init -y
yarn add express
yarn add --dev jest
yarn install
yarn
yarn upgrade
yarn upgrade express
yarn remove express
yarn build
yarn test
yarn start
yarn list
yarn list --depth=0
yarn outdated
yarn upgrade-interactive
pnpm (Fast & Efficient)
npm install -g pnpm
pnpm init
pnpm add express
pnpm add -D jest
pnpm install
pnpm update
pnpm update express
pnpm remove express
pnpm build
pnpm test
pnpm start
pnpm list
pnpm list --depth=0
pnpm outdated
Yarn v3 (Berry)
yarn set version berry
yarn add express
yarn add -D jest
echo "nodeLinker: node-modules" >> .yarnrc.yml
echo "enableGlobalCache: false" >> .yarnrc.yml
Usage Examples
@npm-helper
@npm-helper --init-project
@npm-helper --fix-dependencies
@npm-helper --audit-security
@npm-helper --migrate-to-pnpm
@npm-helper --troubleshoot
Project Initialization
Basic Project Setup
npm init -y
npm install express dotenv
npm install --save-dev \
nodemon \
eslint \
prettier \
jest \
@types/node \
typescript
mkdir -p src tests
touch src/index.js tests/index.test.js
cat > .gitignore << EOF
node_modules/
.env
.env.local
dist/
build/
coverage/
.DS_Store
*.log
EOF
node -v > .nvmrc
TypeScript Project Setup
npm init -y
npm install --save-dev \
typescript \
@types/node \
@types/express \
ts-node \
nodemon
npx tsc --init
cat > tsconfig.json << EOF
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
EOF
npm pkg set scripts.build="tsc"
npm pkg set scripts.dev="nodemon src/index.ts"
npm pkg set scripts.start="node dist/index.js"
Modern ESM Project Setup
{
"name": "my-esm-project",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js",
"scripts": {
"dev": "node --watch src/index.js",
"build": "tsc",
"start": "node dist/index.js",
"test": "node --test"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"@types/node": "^20.10.0",
"typescript": "^5.3.0"
}
}
package.json Configuration
Essential Fields
{
"name": "my-package",
"version": "1.0.0",
"description": "A helpful package",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"engines": {
"node": ">=18.0.0",
"npm": ">=9.0.0"
},
"scripts": {
"dev": "nodemon src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src/**/*.ts --fix",
"format": "prettier --write \"src/**/*.ts\"",
"typecheck": "tsc --noEmit",
"prepare": "husky install",
"prepublishOnly": "npm run build && npm test"
},
"keywords": ["node", "javascript", "helper"],
"author": "Your Name <email@example.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/user/repo.git"
},
"bugs": {
"url": "https://github.com/user/repo/issues"
},
"homepage": "https://github.com/user/repo#readme"
}
Dependency Types
{
"dependencies": {
"express": "^4.18.2",
"dotenv": "^16.3.1"
},
"devDependencies": {
"typescript": "^5.3.0",
"jest": "^29.7.0",
"eslint": "^8.55.0",
"prettier": "^3.1.0"
},
"peerDependencies": {
"react": ">=16.8.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
}
},
"optionalDependencies": {
"fsevents": "^2.3.3"
},
"bundledDependencies": [
"internal-package"
]
}
Scripts Best Practices
{
"scripts": {
"// Development": "",
"dev": "nodemon src/index.ts",
"dev:debug": "nodemon --inspect src/index.ts",
"// Building": "",
"build": "npm run clean && tsc",
"clean": "rm -rf dist",
"prebuild": "npm run lint",
"postbuild": "echo 'Build complete!'",
"// Testing": "",
"test": "jest",
"test:unit": "jest --testPathPattern=unit",
"test:integration": "jest --testPathPattern=integration",
"test:e2e": "jest --testPathPattern=e2e",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"// Linting & Formatting": "",
"lint": "eslint . --ext .ts,.js",
"lint:fix": "eslint . --ext .ts,.js --fix",
"format": "prettier --write \"src/**/*.{ts,js,json}\"",
"format:check": "prettier --check \"src/**/*.{ts,js,json}\"",
"// Type Checking": "",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch",
"// Combined": "",
"validate": "npm run lint && npm run typecheck && npm test",
"ci": "npm run validate && npm run build",
"// Release": "",
"prepare": "husky install",
"prepublishOnly": "npm run ci",
"version": "npm run build && git add -A dist",
"postversion": "git push && git push --tags"
}
}
Dependency Version Management
Semantic Versioning (semver)
{
"dependencies": {
"express": "4.18.2",
"lodash": "^4.17.21",
"axios": "~1.6.0",
"react": ">=16.8.0",
"vue": "<4.0.0",
"moment": "*",
"date-fns": "latest"
}
}
Version Range Examples
^1.2.3
^0.2.3
^0.0.3
~1.2.3
~1.2
~1
1.2.3 - 2.3.4
1.2.x
*
Lock File Management
npm ci
npm install
yarn install --frozen-lockfile
pnpm install --frozen-lockfile
Security and Auditing
Vulnerability Scanning
npm audit
npm audit --json
npm audit --audit-level=moderate
npm audit fix
npm audit fix --force
yarn audit
yarn audit --level moderate
pnpm audit
pnpm audit --audit-level moderate
pnpm audit --fix
Security Best Practices
npm install package@version
npx npm-check-updates
npx ncu -u
npm install
npm outdated
yarn outdated
pnpm outdated
npx snyk test
npx snyk wizard
echo "audit-level=moderate" >> .npmrc
Workspace and Monorepo Management
npm Workspaces
{
"name": "my-monorepo",
"private": true,
"workspaces": [
"packages/*",
"apps/*"
],
"scripts": {
"build": "npm run build --workspaces",
"test": "npm run test --workspaces",
"clean": "npm run clean --workspaces"
}
}
npm install
npm install lodash --workspace=packages/utils
npm run build --workspace=packages/utils
npm run test --workspaces
npm ls --workspaces
Yarn Workspaces
{
"name": "my-monorepo",
"private": true,
"workspaces": {
"packages": [
"packages/*",
"apps/*"
]
}
}
yarn install
yarn workspace @myorg/utils add lodash
yarn workspace @myorg/utils build
yarn workspaces run build
yarn workspaces info
pnpm Workspaces
packages:
- 'packages/*'
- 'apps/*'
pnpm install
pnpm add lodash --filter @myorg/utils
pnpm --filter @myorg/utils build
pnpm -r build
pnpm -r --parallel build
Common Issues & Solutions
Issue: Module Not Found
npm list package-name
rm -rf node_modules package-lock.json
npm install
npm cache clean --force
npm install
echo $NODE_PATH
npm install package-name
Issue: Version Conflicts
npm ls package-name
{
"overrides": {
"package-name": "1.2.3"
}
}
{
"resolutions": {
"package-name": "1.2.3"
}
}
{
"pnpm": {
"overrides": {
"package-name": "1.2.3"
}
}
}
Issue: Peer Dependency Warnings
npm install --legacy-peer-deps
echo "legacy-peer-deps=true" >> .npmrc
npm install peer-dependency-name
Issue: EACCES Permission Errors
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
sudo chown -R $(whoami) ~/.npm
sudo chown -R $(whoami) /usr/local/lib/node_modules
Issue: Corrupted node_modules
rm -rf node_modules package-lock.json
npm cache clean --force
npm install
npm list
npm doctor
df -h
Issue: Slow Installation
npm install -g pnpm
pnpm install
npm install --prefer-offline
npm install --no-optional
npm install --legacy-peer-deps
npm ci
Performance Optimization
.npmrc Configuration
registry=https://registry.npmjs.org/
save-exact=true
progress=false
loglevel=error
engine-strict=true
legacy-peer-deps=false
fund=false
audit=true
Package Installation Optimization
npm ci
npm install --ignore-scripts
npm install --production
npm install --prefer-offline
Bundle Size Optimization
npx webpack-bundle-analyzer
npx package-size lodash moment date-fns
npx find-duplicate-packages
npm install --save-dev bundle-size
npx bundle-size
Publishing Packages
Prepare for Publishing
{
"name": "@myorg/package-name",
"version": "1.0.0",
"description": "Package description",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"README.md",
"LICENSE"
],
"scripts": {
"prepublishOnly": "npm run build && npm test",
"prepare": "npm run build"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
Publishing Workflow
npm login
npm pack --dry-run
npm version patch
npm version minor
npm version major
npm publish
npm publish --access public
npm publish --tag beta
npm view @myorg/package-name
Migration Between Package Managers
npm to Yarn
npm install -g yarn
yarn import
rm package-lock.json
yarn install
npm to pnpm
npm install -g pnpm
pnpm import
rm package-lock.json
pnpm install
Yarn to npm
rm yarn.lock
npm install
Scripts and Automation
Complex Script Examples
{
"scripts": {
"// Parallel execution": "",
"dev": "concurrently \"npm:dev:*\"",
"dev:server": "nodemon src/server.ts",
"dev:client": "vite",
"// Sequential execution": "",
"build": "npm run clean && npm run build:tsc && npm run build:bundle",
"build:tsc": "tsc",
"build:bundle": "webpack",
"// Cross-platform commands": "",
"clean": "rimraf dist",
"copy": "copyfiles -u 1 src/**/*.json dist",
"// Environment-specific": "",
"start:dev": "NODE_ENV=development node dist/index.js",
"start:prod": "NODE_ENV=production node dist/index.js",
"// With arguments": "",
"test": "jest",
"test:file": "jest --",
"// Usage: npm run test:file path/to/test.js"
}
}
Custom npm Scripts
npm run build
npm run test -- --watch
npm run test:file -- src/utils.test.js
npm run build && npm test
npm install --save-dev npm-run-all
npm-run-all --parallel dev:*
Best Practices Summary
Package Management
- Always commit lock files (package-lock.json, yarn.lock, pnpm-lock.yaml)
- Use exact versions in production (
npm install --save-exact)
- Pin Node.js version with .nvmrc
- Use
npm ci in CI/CD for faster, reliable installs
- Keep dependencies minimal (check bundle size)
- Separate dev and production dependencies
Security
- Run
npm audit regularly
- Keep dependencies updated
- Review dependency changes before updating
- Use lock files for reproducible builds
- Don't commit node_modules or .env files
- Use
npx instead of global installs when possible
Performance
- Use pnpm for fastest installation
- Leverage offline cache when possible
- Use
npm ci in CI/CD
- Consider Yarn PnP for zero-installs
- Analyze and optimize bundle size
Project Organization
- Use clear, descriptive script names
- Document complex scripts in README
- Use workspaces for monorepos
- Follow semantic versioning
- Include engines field for Node version requirements
Quick Reference Commands
npm install
npm install <package>
npm install -D <package>
npm install -g <package>
npm ci
npm update
npm update <package>
npm outdated
npm uninstall <package>
npm prune
npm list
npm view <package>
npm search <package>
npm run <script>
npm test
npm start
npm audit
npm audit fix
npm cache clean --force
npm cache verify
npm login
npm publish
npm version <type>
Notes
- Use npm ci in CI/CD for consistent, fast installs
- Always commit lock files to version control
- Prefer exact versions for production dependencies
- Use workspaces for monorepo management
- Regularly audit dependencies for security
- Keep Node.js and package managers updated
- Use .nvmrc to specify Node.js version
- Consider pnpm for better performance and disk usage
- Use semantic versioning for package releases
- Document all custom scripts in README