Are you just starting with Node.js and NPM (Node Package Manager)? NPM is a powerful tool that helps developers manage packages and dependencies in their projects. Here’s a quick guide to the 10 essential NPM commands you should know as a beginner:
1. npm init
Creates a new package.json
file for your project, where all your dependencies and scripts are tracked.
npm init
For a quicker setup, use:
npm init -y
2. npm install
(or npm i
)
Installs the dependencies listed in your package.json
. You can also use it to install new packages:
npm install <package-name>
For a specific version:
npm install <package-name>@<version>
3. npm install -g
Installs a package globally, making it available across your system. Useful for tools like create-react-app
or nodemon
:
npm install -g <package-name>
4. npm uninstall
Removes a package from your project:
npm uninstall <package-name>
5. npm update
Updates all outdated packages in your project:
npm update
To check outdated packages first:
npm outdated
6. npm start
Runs the start
script defined in your package.json
:
npm start
Default scripts can be customized in the scripts
section of your package.json
.
7. npm run
Runs custom scripts from your package.json
. For example, if you have a script named build
:
npm run build
8. npm list
Lists installed dependencies in your project:
npm list
For global packages:
npm list -g
9. npm cache clean
Clears the NPM cache to fix potential issues:
npm cache clean --force
10. npx
Runs CLI tools or executes Node.js packages without installing them globally:
npx create-react-app my-app
Think of it as a one-time-use tool installer and runner.
Final Tips
- Always monitor your dependencies in
package.json
to maintain project organization. - Utilize version control (like Git) to track changes in your project dependencies.
- If you're juggling multiple projects, tools like
nvm
(Node Version Manager) can assist you in managing different Node.js versions.
NPM is a user-friendly tool; the more you use it, the easier it becomes! Let me know which command you found most helpful, or share your own tips in the comments! 🚀
Top comments (0)