Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js webpack configuration detection failed with the following error TypeError [ERR_INVALID_ARG_TYPE]: The "paths[0]" argument must be of type string. Received undefined #880

Open
jantimon opened this issue Jan 30, 2024 · 0 comments · May be fixed by #881
Labels

Comments

@jantimon
Copy link

jantimon commented Jan 30, 2024

Bug Description

Next.js Webpack Configuration Detection Fails in Depcheck

When using depcheck with a Next.js project, the detection of the webpack configuration fails. The error encountered is a TypeError [ERR_INVALID_ARG_TYPE], stating that the "paths[0]" argument must be of type string, but received undefined. This issue seems to stem from the way Next.js handles the .context of a webpack config, which is always a filepath. This mismatch in expectations causes depcheck to fail in processing the webpack configuration correctly.

Code Snippets

Where the dependency is used:

// next.config.js
const { withYak } = require("next-yak/withYak");

const nextConfig = {
  // your next.js config
};

module.exports = withYak(nextConfig);

Versions

  • node -v: 20.9.0
  • depcheck --version: 1.4.7

Possible fix:

I tried the following in our codebase and was able to fix the bug:

diff --git a/node_modules/depcheck/dist/special/webpack.js b/node_modules/depcheck/dist/special/webpack.js
index 7544ccc..76cb237 100644
--- a/node_modules/depcheck/dist/special/webpack.js
+++ b/node_modules/depcheck/dist/special/webpack.js
@@ -102,6 +102,7 @@ function parseWebpackConfig(webpackConfig, deps) {
 }
 async function loadNextWebpackConfig(filepath) {
   const fakeConfig = {
+    context: process.cwd(),
     plugins: [],
     module: {
       rules: []
@@ -119,7 +120,10 @@ async function loadNextWebpackConfig(filepath) {
     }
   };
   const fakeContext = {
-    webpack: _webpack.fakeWebpack,
+    webpack: {
+      context: process.cwd(),
+      ..._webpack.fakeWebpack
+    },
     defaultLoaders: {},
     dir: 'fakePath'
   };
@jantimon jantimon added the bug label Jan 30, 2024
@jantimon jantimon linked a pull request Jan 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant