Here are the main dependency types in a
package.json
file:
Let me know if you need a detailed explanation of any!
Here’s a detailed explanation of the dependency types in a
package.json
file, followed by a comparison:
npm install
or
npm install <package>
"express": "^4.18.2"
react
, express
).
npm install --save-dev
or npm i -D
"eslint": "^8.50.0"
jest
,
webpack
, eslint
).
"react": ">=17.0.0"
react
).
npm install
(like
dependencies
)
"fsevents": "^2.3.2"
fsevents
for file
watching on macOS).
npm pack
or npm publish
.
[ "lodash", "axios" ]
Type | Installed in Production? | Installed in Development? | Managed Automatically? | Purpose |
---|---|---|---|---|
dependencies | ✅ Yes | ✅ Yes | ✅ Yes | Core functionality for production. |
devDependencies | ❌ No | ✅ Yes | ✅ Yes | Tools for development/testing. |
peerDependencies | ⚠️ No (manual install) | ⚠️ No (manual install) | ❌ No | Compatibility with a host package. |
optionalDependencies | ✅ Yes | ✅ Yes | ✅ Yes | Enhance functionality; safe to fail install. |
bundledDependencies | ✅ Yes | ✅ Yes | ✅ Yes (when published) | Guarantee specific packages are bundled. |
Production vs. Development:
dependencies
are for production;
devDependencies
are for development only.
Compatibility:
peerDependencies
don’t install automatically and
are used to specify compatibility, unlike
dependencies
.
Fault Tolerance:
optionalDependencies
ignore installation errors,
unlike others.
Bundling:
bundledDependencies
are packaged directly with your
app, whereas others are fetched from the registry during
installation.
Let me know if you want real-world examples or best practices!
To change an already installed dependency into a
devDependency
in a Node.js project, follow these steps:
npm uninstall <package-name>
This removes the dependency from both node_modules
and
your package.json
file.
npm install --save-dev <package-name>
This will add the dependency under the
devDependencies
section in your
package.json
.
package.json
Directly
package.json
file in a text editor.dependencies
section.
"eslint": "^8.50.0"
)
and paste it under the devDependencies
section.
package.json
with the node_modules
:
npm install
npm install
with --save-dev Flag
You can install the same dependency with the
--save-dev
flag, and it will automatically move to
devDependencies
. For example:
npm install --save-dev <package-name>
This ensures the dependency is placed in the correct section, even if it was already installed.
After making changes, confirm the dependency has been moved to
devDependencies
:
package.json
file to ensure the dependency is
listed under devDependencies
.
npm ls <package-name>
to verify the package
is still installed.
Let me know if you need help with any of these steps!