ncc: "require is not defined in ES module scope" and mixed require/import using ESM with native modules

I’m trying to compile an ES module ("type": "module") that uses tree-sitter. The compiled distributable winds up importing createRequire as expected, but still tries to load native code via old-fashioned require.

package.json
{
  "name": "ncctest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "tree-sitter": "^0.19.0",
    "tree-sitter-javascript": "^0.19.0"
  },
  "devDependencies": {
    "@vercel/ncc": "^0.31.1"
  }
}
index.js
'use strict';

import * as TreeSitter from 'tree-sitter';
import JavaScript from 'tree-sitter-javascript';

const parser = TreeSitter.default();

parser.setLanguage(JavaScript);
> npx ncc build index.js
> node dist/index.js
file:///home/dian/work/ncctest/dist/index.js:802
module.exports = require(__nccwpck_require__.ab + "build/Release/tree_sitter_runtime_binding.node")
^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/home/dian/work/ncctest/dist/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at Object.460 (file:///home/dian/work/ncctest/dist/index.js:802:1)
    at __nccwpck_require__ (file:///home/dian/work/ncctest/dist/index.js:860:41)
    at Object.525 (file:///home/dian/work/ncctest/dist/index.js:35:13)
    at __nccwpck_require__ (file:///home/dian/work/ncctest/dist/index.js:860:41)
    at file:///home/dian/work/ncctest/dist/index.js:880:69
    at file:///home/dian/work/ncctest/dist/index.js:891:3
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

The offending require in question:

/***/ 460:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

module.exports = require(__nccwpck_require__.ab + "build/Release/tree_sitter_runtime_binding.node")

/***/ }),

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 33
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

same issue

For my use case, I was using the sqlite and sqlite3 modules. I was able to avoid this error by leaving them external in my build command:

ncc build src/index.ts -m -s --target es2020 -e sqlite3 -e sqlite

I’ve got minify and source maps turned on. The relevant part for other folks should be the -e external flag.

I found this piece of code in my build:

Thanks for the tip. In my case I hit this error because in my source code I had

import { App } from `./App`;

and the build contained

module.exports = eval("require")("./App");

changing it to

import { App } from `./App.js`;

fixed the problem 😕

I found this piece of code in my build:

module.exports = eval("require")("@actions/cache");

This happened because @actions/cache was not installed in my dependencies.

I suggest looking into your build for any require calls.

Either way NCC should not output such eval("require") calls in ESM envs.

Seems like I was able to apply a workaround for this problem: polyfilling require in ESM like this:

// File: "polyfill-require-in-esm.mjs"

/**
 * This ESM module polyfills "require".
 * It is needed e.g. when bundling ESM scripts via "@vercel/ncc" because of https://github.com/vercel/ncc/issues/791.
 */
import { createRequire } from 'node:module';
import url from 'node:url';

const __filename = url.fileURLToPath(import.meta.url);
globalThis.require = createRequire(__filename);
// File: "my-script.mjs"
import './polyfill-require-in-esm.mjs';

// now do everything else, importing other packages and stuff

Same error when using ES6 in a GitHub action based on https://github.com/actions/javascript-action @vercel/ncc version 0.36.1

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/runner/_work/_actions/[...]/dist/status-check/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at eval (eval at 553 (file:///runner/_work/_actions/[...]/dist/webpack:/javascript-action/node_modules/@vercel/ncc/dist/ncc/@@notfound.js:1:1), <anonymous>:1:1)
    at Object.553 (file:///runner/_work/_actions/[...]/dist/webpack:/javascript-action/node_modules/@vercel/ncc/dist/ncc/@@notfound.js:1:1)
    at __nccwpck_require__ (file:///runner/_work/_actions/[...]/dist/webpack:/javascript-action/webpack/bootstrap:21:1)
    at file:///runner/_work/_actions/[...]/dist/status-check/index.js:2827:78
    at file:///runner/_work/_actions/[...]/dist/status-check/index.js:2859:3
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at loadESM (node:internal/process/esm_loader:91:5)
    at handleMainPromise (node:internal/modules/run_main:65:12)

Any solution?

This issue still happens, no official fixes yet?