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:
- Ensure your project has a valid
package.jsonfile that includes thedevDependenciesfield, listing all modules required for development. - Open your terminal or command prompt.
- Navigate to the project directory containing the
package.jsonfile. - Execute the following command:
bashnpm install --only=dev
Or use the abbreviated form:
bashnpm 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 installwithout specifying--only=dev, thenode_modulesdirectory may already containdependencies. In this case, you may need to clean the existingnode_modulesdirectory first. Usenpm ci --only=devto 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.