NPM, the default package manager for Node.js, plays a vital role in managing and distributing JavaScript code and libraries. Understanding its commands is crucial for any Node.js developer. This comprehensive guide delves into 25 commonly used npm commands, providing detailed explanations, code examples, and practical applications.
1. npm init
Purpose: Initializes a new npm project by creating a package.json file.
Syntax:
npm init
Example:
$ npm init
2. npm install
Purpose: Installs a package from the npm registry or a local directory.
Syntax:
npm install <package name>
Example:
$ npm install express
3. npm uninstall
Purpose: Uninstalls a package.
Syntax:
npm uninstall <package name>
Example:
$ npm uninstall express
4. npm update
Purpose: Updates all installed packages to their latest versions.
Syntax:
npm update
Example:
$ npm update
5. npm outdated
Purpose: Lists outdated installed packages.
Syntax:
npm outdated
Example:
$ npm outdated
Package Current Wanted Latest
express 4.17.3 5.0.0 5.0.0
body-parser 1.19.1 2.0.0 2.0.0
6. npm audit
Purpose: Checks for security vulnerabilities in installed packages.
Syntax:
npm audit
Example:
$ npm audit
7. npm run-script
Purpose: Runs a script defined in the package.json file.
Syntax:
npm run-script <script name>
Example:
$ npm run-script start
8. npm start
Purpose: Runs the default script specified in the package.json file, typically used to start the application.
Syntax:
npm start
Example:
$ npm start
9. npm test
Purpose: Runs the test script specified in the package.json file, typically used to run unit tests.
Syntax:
npm test
Example:
$ npm test
10. npm publish
Purpose: Publishes a package to the npm registry.
Syntax:
npm publish
Example:
$ npm publish
11. npm link
Purpose: Creates a symlink between a local package and the global node_modules directory.
Syntax:
npm link <package name>
Example:
$ npm link my-package
12. npm unlink
Purpose: Removes a symlink created by npm link.
Syntax:
npm unlink <package name>
Example:
$ npm unlink my-package
13. npm list
Purpose: Lists installed packages.
Syntax:
npm list
Example:
$ npm list
├─┬ express@5.0.0
│ └── accepts@~1.3.8
├─┬ body-parser@2.0.0
│ ├── bytes@3.1.2
│ ├── content-type@~1.0.4
│ ├── debug@2.6.9
│ ├── depd@~2.0.0
│ ├── destroy@~1.0.4
...
14. npm search
Purpose: Searches for packages on the npm registry.
Syntax:
npm search <package name>
Example:
$ npm search react
15. npm view
Purpose: Displays information about a package, such as its description, author, and dependencies.
Syntax:
npm view <package name>
Example:
$ npm view express
16. npm install -g
Purpose: Installs a package globally, making it available to all projects on your system.
Syntax:
npm install -g <package name>
Example:
$ npm install -g create-react-app
17. npm uninstall -g
Purpose: Uninstalls a globally installed package.
Syntax:
npm uninstall -g <package name>
Example:
$ npm uninstall -g create-react-app
18. npm config
Purpose: Gets or sets npm configuration options.
Syntax:
npm config [<key>] [<value>]
Example:
$ npm config get registry
https://registry.npmjs.org/
19. npm cache clean
Purpose: Clears the npm cache. If this fails please also include "--force" .
Syntax:
npm cache clean
Example:
$ npm cache clean
20. npm dedupe
Purpose: Removes duplicate packages and their dependencies from the node_modules directory.
Syntax:
npm dedupe
Example:
$ npm dedupe
21. npm init -y
Purpose: Initializes a new npm project with default settings.
Syntax:
npm init -y
Example:
$ npm init -y
22. npm install --save
Purpose: Installs a package and adds it as a dependency in the package.json file.
Syntax:
npm install --save <package name>
Example:
$ npm install --save express
23. npm install --save-dev
Purpose: Installs a package and adds it as a development dependency in the package.json file.
Syntax:
npm install --save-dev <package name>
Example:
$ npm install --save-dev webpack
24. npm ci
Purpose: Installs packages based on the package-lock.json file, ensuring consistent installations across different environments.
Syntax:
npm ci
Example:
$ npm ci
25. npm help
Purpose: Displays help information for a specific command or npm in general.
Syntax:
npm help <command>
Example:
$ npm help install
Conclusion
Mastering these 25 npm commands will empower you to effectively manage Node.js packages and dependencies. By leveraging these commands, you can install and update packages, resolve vulnerabilities, publish your own packages, and streamline your development workflow. Remember to experiment with these commands and familiarize yourself with their options and capabilities.
Comments
Post a Comment
Oof!