Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

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

@kachkaev

Description

@kachkaev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions