0% found this document useful (0 votes)
34 views

NPM Commands

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

NPM Commands

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

NPM (Node Package Manager) comes with a set of powerful commands for managing

dependencies, running scripts, publishing packages, and configuring the environment


in Node.js projects. Below is a list of essential NPM commands and their uses.

---

### 1. **Installing Packages**

- **`npm install`**: Installs all dependencies listed in `package.json`.


```bash
npm install
```

- **`npm install <package>`**: Installs a specific package and adds it to


`dependencies` in `package.json`.
```bash
npm install express
```

- **`npm install <package> --save-dev`**: Installs a package and adds it to


`devDependencies`.
```bash
npm install jest --save-dev
```

- **`npm install <package>@<version>`**: Installs a specific version of a package.


```bash
npm install [email protected]
```

- **`npm install -g <package>`**: Installs a package globally, making it available


across the system.
```bash
npm install -g nodemon
```

---

### 2. **Removing Packages**

- **`npm uninstall <package>`**: Removes a package from `node_modules` and updates


`package.json`.
```bash
npm uninstall express
```

- **`npm uninstall <package> --save-dev`**: Removes a package from


`devDependencies`.
```bash
npm uninstall jest --save-dev
```

- **`npm uninstall -g <package>`**: Uninstalls a global package.


```bash
npm uninstall -g nodemon
```

---
### 3. **Updating Packages**

- **`npm update`**: Updates all packages in `node_modules` to the latest version


allowed by the version range specified in `package.json`.
```bash
npm update
```

- **`npm update <package>`**: Updates a specific package.


```bash
npm update lodash
```

- **`npm outdated`**: Lists all packages with available updates, showing the
current, wanted, and latest versions.
```bash
npm outdated
```

---

### 4. **Running Scripts**

- **`npm run <script>`**: Runs a custom script defined in the `scripts` section of
`package.json`.
```bash
npm run build
```

- **`npm test`**: Shortcut for running the `test` script.


```bash
npm test
```

- **`npm start`**: Shortcut for running the `start` script.


```bash
npm start
```

---

### 5. **Package Management Commands**

- **`npm init`**: Starts a wizard to create a `package.json` file.


```bash
npm init
```

- **`npm init -y`**: Automatically creates a `package.json` file with default


settings.
```bash
npm init -y
```

- **`npm list`**: Lists installed packages and their dependencies in the current
project.
```bash
npm list
```
- **`npm list -g`**: Lists all globally installed packages.
```bash
npm list -g
```

---

### 6. **Publishing and Managing Versions**

- **`npm version <version>`**: Updates the version in `package.json` (major, minor,


patch) and commits it.
```bash
npm version patch
```

- **`npm publish`**: Publishes the package to the NPM registry (you need an NPM
account).
```bash
npm publish
```

- **`npm unpublish <package>`**: Removes a published package (only available within


24 hours of publishing).
```bash
npm unpublish my-package
```

---

### 7. **Configuration and Cache Management**

- **`npm config set <key> <value>`**: Sets an NPM configuration variable.


```bash
npm config set registry https://my.custom.registry/
```

- **`npm config get <key>`**: Gets the value of an NPM configuration variable.
```bash
npm config get registry
```

- **`npm cache clean --force`**: Clears the NPM cache (useful for resolving
issues).
```bash
npm cache clean --force
```

---

### 8. **Audit and Security**

- **`npm audit`**: Analyzes dependencies for security vulnerabilities.


```bash
npm audit
```

- **`npm audit fix`**: Automatically fixes vulnerabilities if patches are


available.
```bash
npm audit fix
```

---

### 9. **Miscellaneous Commands**

- **`npm root`**: Displays the path to the `node_modules` folder.


```bash
npm root
```

- **`npm help`**: Opens the help documentation for NPM commands.


```bash
npm help install
```

Each of these commands plays a role in managing packages, dependencies, and


development workflows, making NPM a powerful tool for JavaScript projects.

You might also like