#1163·ncc

ESM mode still uses "require" within an ESM module

Author: onigoetzCreated Feb 8, 2024Updated Sep 9, 2025
Labelsbug

Hi,

I'm trying to bundle my dependencies within an ESM package I get mixed results depending if the module I compile is using ESM or CJS.

Here is an example of three packages I'm trying to bundle, each in their separate file;

  • is-fullwidth-code-point, CJS module
  • slice-ansi, ESM module, depends on is-fullwidth-code-point
  • string-width, CJS module, depends on is-fullwidth-code-point

When compiling these three modules they all compile fine, and create a package.json containing { "type": "module" }.

However, within string-width, NCC (I guess because of the asset relocator) require calls are transformed to the following:

/***/ 695:
/***/ ((module) => {

module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("../is-fullwidth-code-point/index.mjs");

/***/ }),

Which fails when we try to run it since the result is a require of an ESM module:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/is-fullwidth-code-point/index.mjs not supported.
Instead change the require of /Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/is-fullwidth-code-point/index.mjs to a dynamic import() which is available in all CommonJS modules.
    at 695 (file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:74:67)
    at __nccwpck_require__ (file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:107:41)
    at 871 (file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:22:30)
    at __nccwpck_require__ (file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:107:41)
    at file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:146:70
    at file:///Users/onigoetz/Sites/Libs/crafty/packages/crafty-preset-stylelint/dist/string-width/index.mjs:148:3

is there a way to tell the asset relocator that the destination is an ESM module and it should let webpack hoist the dependency ?