Last updated: Mar 7, 2024
Reading time·4 min
devDependencies
in your test filesThe ESLint error "'X' should be listed in the project's dependencies, not devDependencies" is shown for 2 main reasons:
devDependencies
object into a file.--save-dev
flag.To resolve the error, update the import/no-extraneous-dependencies
rule in
your .eslintrc.js
file.
Here is the complete error message.
'html-webpack-plugin' should be listed in the project's dependencies, not devDependencies. (import/no-extraneous-dependencies) '@storybook/react' should be listed in the project's dependencies, not devDependencies.eslintimport/no-extraneous-dependencies '@testing-library/react' should be listed in the project's dependencies, not devDependencies 'chai' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 'chai-enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 'enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
Open your .eslintrc.js
file and update the import/no-extraneous-dependencies
as follows.
module.exports = { "rules": { "import/no-extraneous-dependencies": [ "error", {"devDependencies": true} ] }, };
If you use a .eslintrc.json
file or simply .eslintrc
, make sure to double
quote all properties and values that are of type string.
{ "rules": { "import/no-extraneous-dependencies": [ "error", {"devDependencies": true} ] } }
When the devDependencies
property in the import/no-extraneous-dependencies
rule is set to false
, the rule shows an error when modules from the
devDependencies
object in package.json
are imported into your files.
Conversely, if the devDependencies
property is set to true
, then no error is
thrown when modules from the devDependencies
object are imported.
You often need to import modules from your devDependencies
object when
testing.
Setting the property to true
should resolve the issue.
If the error persists, try to restart your code editor.
You might also have to restart your Node.js server for the change to take effect.
devDependencies
in your test filesIn some cases, you might only want to allow imports of devDependencies
in your
test files.
If that's the case, set the devDependencies
array to an array of glob patterns
that match your test files.
module.exports = { "rules": { "import/no-extraneous-dependencies": [ "error", { "devDependencies": [ "test.{ts,tsx,js,jsx}", "test-*.{ts,tsx,js,jsx}", "**/*{.,_}{test,spec}.{ts,tsx,js,jsx}", "**/jest.config.{ts,js}", "**/jest.setup.{ts,js}", "**/*.stories.*", "**/.storybook/**/*.*" ] } ] }, };
If you use a .eslintrc.json
or a .eslintrc
file, make sure to double-quote
all properties and values.
{ "rules": { "import/no-extraneous-dependencies": [ "error", { "devDependencies": [ "test.{ts,tsx,js,jsx}", "test-*.{ts,tsx,js,jsx}", "**/*{.,_}{test,spec}.{ts,tsx,js,jsx}", "**/jest.config.{ts,js}", "**/jest.setup.{ts,js}", "**/*.stories.*", "**/.storybook/**/*.*" ] } ] } }
The configuration matches TypeScript and JavaScript test files.
You can add or remove glob patterns depending on your use case.
When using this approach, you won't get an error when importing
devDependencies
in your test files.
Conversely, if you try to import a module from your devDependencies
object in
your non-test files, you would still get an error.
The error "'X' should be listed in the project's dependencies, not devDependencies" is also shown if you install a module that is used in production in development mode.
This happens when you use the --save-dev
(npm) or --dev
(yarn) flag when
installing a module.
Here is an example.
npm install axios --save-dev yarn add axios --dev
The axios
package should not be installed as a development dependency because
it is used in production and gets added to your production bundle.
If you open your package.json
after running one of the commands above, you
will see that the axios
package is added to your devDependencies
object.
{ "devDependencies": { "axios": "^1.4.0" } }
If you import a module in non-test files then the module should be in your
dependencies
object and not devDependencies
in package.json
.
--save-dev
flag.# uninstall the module using NPM npm uninstall axios # uninstall the module using YARN yarn remove axios
Make sure to replace axios
with the specific module that caused your issue.
Once the module is uninstalled, it will be removed from your devDependencies
object.
--save-dev
flag.# install the module using NPM npm install axios # install the module using YARN yarn add axios
Now the module will be added to your dependencies
object.
{ "dependencies": { "axios": "^1.4.0", } }
If that didn't work:
devDependencies
object to your
dependencies
object.npm install
.# with NPM npm install # with YARN yarn install
After you move all of the offending modules to your dependencies
object and
rerun npm install
, the issue should be resolved.
If the issue persists:
devDependencies
object to your
dependencies
object.For example, the following:
// ⛔️ incorrect { "devDependencies": { "axios": "^1.4.0", } }
Becomes the following:
// ✅ correct { "dependencies": { "axios": "^1.4.0", } }
node_modules
directory and your
package-lock.json file.If you are on macOS or Linux, run the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # 👇️ clean npm cache npm cache clean --force # 👇️ install packages npm install # or using yarn yarn install
If you are on Windows, run the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # 👇️ clean npm cache npm cache clean --force # 👇️ install packages npm install # or using yarn yarn install
If the issue persists, restart your code editor.
You can learn more about the related topics by checking out the following tutorials: