96. npm install *** -D/-S的区别和作用
When using npm install
the command to install a dependency package, you can use the -D
or --save-dev
flag to save the dependency as a dependency of the development environment, and use -S
the or --save
flag to save the dependency as a dependency of the production environment.
1. -D
Or --save-dev
: development environment dependency
- These dependencies are usually tools, libraries, or aids for project development, building, testing, and debugging.
- They work during development, but are usually not included in the final production environment when the project is deployed or released.
- Example: Installing
webpack
as a development environment dependency:
npm install webpack --save-dev
2. -S
Or --save
: production environment dependent
- These dependencies are the core libraries or functional modules required by the project at runtime.
- They are required for the project to function properly and will be included in the final production environment.
- Example: Installing
axios
as a production dependency:
npm install axios --save
In package.json
the file, dependencies will be stored in different fields:
- Development environment dependencies are held in
devDependencies
the field. - Production dependencies are stored in
dependencies
the field.
Example package.json
file:
{
"name": "my-project",
"version": "1.0.0",
"devDependencies": {
"webpack": "^5.0.0"
},
"dependencies": {
"axios": "^0.21.1"
}
}
In the example above, webpack
is stored in devDependencies
the field and axios
is stored in dependencies
the field.
By explicitly saving the dependencies in different fields, the dependencies of the development environment and the production environment can be clearly distinguished, which is convenient for management and maintenance.
Using the -D
and -S
flags ensures that your project's dependencies are saved correctly in the appropriate fields, and that you npm install
don't confuse development and production dependencies when using install dependencies. This helps improve maintainability and reliability of the project.