NPM (Node Package Manager) commands are essential tools for developers working with JavaScript and Node.js, enabling efficient management of project dependencies, scripts, and configurations. Here are some key NPM commands and their uses:
1. npm init
: Initializes a new Node.js project by creating a package.json
file, which holds metadata about the project and its dependencies. The npm init
command can be run with -y
to accept default settings automatically.
2. npm install
or npm i
: Installs all the dependencies listed in the package.json
file. This command is crucial for setting up a project's development environment.
3. npm install <package>
: Installs a specific package and adds it to the dependencies
section of the package.json
file. Adding the --save-dev
flag will save the package to the devDependencies
section, which is useful for packages required only during development.
4. npm update
: Updates all the packages listed in the package.json
file to their latest versions that satisfy the specified version ranges.
5. npm uninstall <package>
: Removes a specified package from the project and updates the package.json
file accordingly.
6. npm run <script>
: Executes a script defined in the scripts
section of the package.json
file. For example, npm run test
would run the test script specified in the project's configuration.
7. npm start
: A shorthand for running the start
script defined in the package.json
file. It's commonly used to start the main application.
8. npm test
: A shorthand for running the test
script. This command is used to execute the project's test suite.
9. npm ls
: Lists all the installed packages in the current project, showing their dependencies in a tree structure.
10. npm outdated
: Checks for any outdated packages in the project's dependencies and provides information on available updates.
11. npm audit
: Scans the project for vulnerabilities in dependencies and provides a report, helping developers address security issues.
12. npm cache clean
: Clears the NPM cache, which can resolve issues with package installations and updates.
NPM commands streamline the management of JavaScript and Node.js projects, making it easier to handle dependencies, run scripts, and maintain project configurations. These commands are essential tools for modern JavaScript development.