When using npm (Node Package Manager) to install dependencies, you can specify how dependency records are saved by adding parameters after the command. Here are some commonly used save option parameters:
-
--saveor-S: This parameter has been deprecated in npm 5+ because npm 5 defaults to saving dependencies to thedependenciessection of thepackage.jsonfile. Before npm 5, dependencies installed with--saveare added to thedependenciessection ofpackage.json, indicating they are required for the project to run at runtime. -
--save-devor-D: This parameter saves dependencies to thedevDependenciessection of thepackage.jsonfile. Typically, these dependencies are only needed during development, such as for build tools and testing libraries, and are not used in production. -
--save-optionalor-O: Dependencies installed with this parameter are added to theoptionalDependenciessection ofpackage.json. These dependencies are optional for the project; even if they fail during installation, the overall process does not fail. -
--no-save: When installing dependencies with this option, npm will not modify thepackage.jsonorpackage-lock.jsonfiles. This is commonly used for temporarily installing dependencies without altering the current dependency state of the project. -
--save-exactor-E: This parameter installs a specific version of the dependency and records the exact version number inpackage.jsoninstead of using version ranges. -
--save-peer: This parameter was not available in early versions of npm but was added in newer versions. It is used to explicitly mark dependencies as peer dependencies and add them to thepeerDependenciesobject.
As an example, if you want to install a library named lodash and use it as a development dependency for the project, you can use the following command:
bashnpm install lodash --save-dev
This will add lodash to the devDependencies section of the project's package.json file. If you want to install a specific version of lodash and ensure every developer in the project uses the exact version, you can use:
bashnpm install lodash@4.17.15 --save-exact