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

Unable to analyze next.config.js in a ESM package #762

Closed
kachkaev opened this issue Dec 7, 2022 · 2 comments · Fixed by #786
Closed

Unable to analyze next.config.js in a ESM package #762

kachkaev opened this issue Dec 7, 2022 · 2 comments · Fixed by #786
Labels

Comments

@kachkaev
Copy link

kachkaev commented Dec 7, 2022

Bug Description

Next.js supports "type": "module" in package.json. When this property is set, next.config.js is treated as ESModule, so it needs to be import-edrather thanrequire-d. depcheck` does not take this into account and outputs an error:

Next.js webpack configuration detection failed with the following error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/project/next.config.js from /path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/special/webpack.js not supported.
Instead change the require of next.config.js in /path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/special/webpack.js to a dynamic import() which is available in all CommonJS modules.
    at loadNextWebpackConfig (/path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/special/webpack.js:153:24)
    at parseWebpack (/path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/special/webpack.js:187:27)
    at getDependencies (/path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/check.js:71:24)
    at /path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/check.js:97:32
    at Array.map (<anonymous>)
    at checkFile (/path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/check.js:97:18)
    at ReaddirpStream.<anonymous> (/path/to/.npm/_npx/ec702ce30756237c/node_modules/depcheck/dist/check.js:126:24)
    at ReaddirpStream.emit (node:events:513:28)
    at ReaddirpStream._read (/path/to/.npm/_npx/ec702ce30756237c/node_modules/readdirp/index.js:143:22) {
  code: 'ERR_REQUIRE_ESM'
} Support for this feature is new and experimental, please report issues at https://github.com/depcheck/depcheck/issues

This should be fixable by replacing require with await import on this line:

const nextConfig = require(filepath); // eslint-disable-line global-require

As a result, loadNextWebpackConfig needs to become async. Replacing require should be backwards compatible because CommonJS modules can be import-ed (but not vice versa).

Code snippets

Discovered in https://github.com/blockprotocol/blockprotocolapps/site

cd /tmp
git clone https://github.com/blockprotocol/blockprotocol.git

cd blockprotocol
yarn install

cd apps/site; npx depcheck@latest; cd -

Versions

  • node -v: 16.18.0
  • npm -v: 8.19.2
  • npx depcheck@latest --version: 1.4.3

Extra info

When looking into this issue, it might also worth adding support for next.config.mjs. This file can be used in projects that don’t define "type": "module" in package.json.

@kachkaev kachkaev added the bug label Dec 7, 2022
@kachkaev kachkaev changed the title Unable to analyze next.config.js in a ESM package. Unable to analyze next.config.js in a ESM package Dec 7, 2022
@Yukigamine
Copy link

I'm still seeing this warning in two different repositories on the latest version of depcheck. Any ideas?

@rumpl
Copy link
Member

rumpl commented Oct 18, 2023

@Yukigamine could you open a new issue and give us all the info we need to debug this remotely? Like the output of depcheck --json and such, the template will guide you 👍

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.

3 participants