乐闻世界logo
搜索文章和话题

How to install only " devDependencies " using npm

1个答案

1

When using npm in JavaScript projects, you often need to install different types of dependencies, primarily categorized as dependencies and devDependencies. dependencies are essential for the project to run, while devDependencies are required during development, such as testing frameworks and build tools.

To install only devDependencies in your project, follow these steps:

  1. Ensure your project has a valid package.json file that includes the devDependencies field, listing all modules required for development.
  2. Open your terminal or command prompt.
  3. Navigate to the project directory containing the package.json file.
  4. Execute the following command:
bash
npm install --only=dev

Or use the abbreviated form:

bash
npm install --only=dev

This command makes npm ignore modules listed in dependencies and installs only those specified in devDependencies.

Example

Suppose your package.json file contains the following:

json
{ "name": "example-project", "version": "1.0.0", "devDependencies": { "webpack": "^4.44.2", "babel-core": "^6.26.3" } }

Running npm install --only=dev in the project's root directory will install webpack and babel-core, without installing any modules listed in dependencies.

Notes

  • Ensure your network connection is stable, as npm requires downloading modules from remote repositories.
  • If you previously ran npm install without specifying --only=dev, the node_modules directory may already contain dependencies. In this case, you may need to clean the existing node_modules directory first. Use npm ci --only=dev to clean and reinstall only the development dependencies.

By doing this, you can ensure only dependencies necessary for development are installed, helping to maintain a clean and manageable development environment.

2024年8月2日 14:34 回复

你的答案