Skip to content

Instantly share code, notes, and snippets.

@jdomingu19
Last active April 10, 2026 00:10
Show Gist options
  • Select an option

  • Save jdomingu19/d24dbba9d5744f15ad7ce30d2e2d7c16 to your computer and use it in GitHub Desktop.

Select an option

Save jdomingu19/d24dbba9d5744f15ad7ce30d2e2d7c16 to your computer and use it in GitHub Desktop.

Useful NPM Commands

A curated list of npm commands, organized from beginner to advanced. Each command includes a short explanation and a ready‑to‑copy snippet.

header_hello_nodejs

Basic Commands

Getting started with npm often means learning the essential commands that help you initialize projects, install or remove packages, and check your environment setup. These commands form the foundation of everyday Node.js development, giving you the ability to quickly scaffold applications, manage dependencies, and verify versions. Think of them as the “first toolbox” you’ll reach for whenever you begin working with a new project.

1. Initialize a new project: Create a package.json file interactively.

npm init

2. Quick project initialization: Generate a default package.json without prompts.

npm init -y

3. Install a local package: Add a dependency to your project.

npm init -y

4. Install a package globally: Make a tool available system‑wide

npm install -g <package-name>

5. Uninstall a package: Remove a dependency from your project.

npm uninstall <package-name>

6. List installed packages: Show all dependencies in the current project.

npm list

7. List globally installed packages: Check what’s installed system‑wide.

npm list -g --depth=0

8. Check npm version: Verify the installed npm version.

npm list -g --depth=0

9. Check Node.js version: Verify the installed node version.

node -v

10. Run a script: Execute a script defined in package.json.

npm run <script-name>

Intermediate Commands

Once you are comfortable with the basics, npm offers a set of intermediate commands that help you manage dependencies more effectively, keep your project secure, and streamline your workflow. These commands go beyond simple installation and removal, allowing you to update packages, audit for vulnerabilities, inspect metadata, and control binary paths. They are especially useful when maintaining larger projects or collaborating with teams, as they ensure consistency, security, and efficiency across your development environment.

11. Install dev dependencies: Add packages needed only for development.

npm install --save-dev <package-name>

12. Update all packages: Bring dependencies to their latest compatible versions.

npm update

13. Check outdated packages: See which dependencies have newer versions available.

npm outdated

14. Audit dependencies: Scan for security vulnerabilities.

npm audit

15. Fix audit issues automatically: Attempt to resolve vulnerabilities.

npm audit fix

16. View binary paths: Show where global executables are stored.

npm bin -g

17. View local binary paths: Show executables for the current project.

npm bin

18. View package details: Inspect metadata for a specific package.

npm view <package-name>

19. Check package size: See the installed size of a dependency.

npm ls --json | npm run size

20. Clear npm cache: Remove cached files to fix issues.

npm cache clean --force

Advanced Commands

At the advanced level, npm provides powerful commands that let you go beyond everyday dependency management. These tools enable you to control versions precisely, install packages from unconventional sources, link local projects for testing, and even publish your own modules to the registry. They are designed for developers who want to customize workflows, manage private registries, or contribute back to the ecosystem. Mastering these commands gives you full control over how your Node.js projects interact with the broader JavaScript community.

21. Install a specific version: Pin a dependency to an exact version.

npm install <package-name>@<version>

22. Install from GitHub: Use a repository as a dependency.

npm install github:<user>/<repo>

23. Install from a tarball: Add a package from a .tgz file.

npm install <path-to-tarball>

24. Link local packages: Symlink a local project for testing.

npm link

25. Unlink local packages: Remove symlinked dependencies.

npm unlink <package-name>

26. Publish a package: Upload your project to the npm registry.

npm publish

27. Unpublish a package: Remove a package from the registry (use with caution).

npm unpublish <package-name>

28. Set registry URL: Switch to a custom npm registry.

npm set registry <url>

29. View npm config: Inspect current configuration values.

npm config list

30. Run scripts with lifecycle hooks: Use built‑in hooks like preinstall or postinstall.

npm run <hook-name>

Built with '\u{2665}' (♥) by Jesús Domínguez @jdomingu19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment