What is NPM?
NPM stands for Node Package Manager. It comes with Node.js, a tool that runs JavaScript outside the browser. A package is a ready-made piece of code that someone else wrote. NPM downloads and manages these packages for you, like an app store for code.
Check that NPM is installed
node -v
npm -v
These print the version numbers of Node.js and NPM. If you see an error, install Node.js first.
Start a project
npm init
This asks a few questions and creates a package.json file. That file is the list of your project's details and packages. Use npm init -y to accept all defaults.
Install packages
npm install express
This downloads the package named express into a node_modules folder and adds it to package.json.
npm install
Without a name, this installs every package listed in package.json. Use it after you download someone's project.
npm install --save-dev nodemon
This installs a package that you only need while building, not on the live site.
npm install -g pm2
The -g flag installs the package globally. That means it works from anywhere on the computer. You may need admin rights.
Remove and update
npm uninstall express
This removes a package from your project.
npm update
This updates packages to newer versions allowed by package.json. Take a backup first, because updates can break code.
npm outdated
This lists packages that have newer versions.
See what is installed
npm list
This shows installed packages. Add --depth=0 to show only the top level.
Run your scripts
npm start
npm test
npm run build
These run the scripts defined in the scripts part of package.json. npm run name works for any script name.
Fix security problems
npm audit
npm audit fix
The first checks your packages for known security problems. The second tries to fix them.
Clean up
npm cache clean --force
This clears NPM's download cache. Use it when installs act strangely.
node_modules. NPM can recreate that folder any time with npm install.Quick recap
npm initstarts a project.npm install nameadds a package;npm installadds all.npm uninstallremoves;npm updateupdates.npm startandnpm runrun scripts.npm auditchecks for security problems.