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
- fix: `require is not defined in ES module scope` - https://github.com/vercel/ncc/issues/791\#issuecomment-942454663 — committed to flex-development/trext by unicornware 5 years ago
- fix: `require is not defined in ES module scope` - https://github.com/vercel/ncc/issues/791\#issuecomment-942454663 — committed to flex-development/trext by unicornware 5 years ago
- fix: move main.js to root dir, fix ncc build error ref: https://github.com/vercel/ncc/issues/791 — committed to akkuman/gitea-release-action by akkuman 3 years ago
- Probing with vercel/ncc/issues/791 suggestion — committed to ldng/fvtt-gh-actions by ldng 2 years ago
same issue
For my use case, I was using the
sqliteandsqlite3modules. I was able to avoid this error by leaving them external in my build command:I’ve got minify and source maps turned on. The relevant part for other folks should be the
-eexternal flag.Thanks for the tip. In my case I hit this error because in my source code I had
and the build contained
changing it to
fixed the problem 😕
I found this piece of code in my build:
This happened because
@actions/cachewas not installed in my dependencies.I suggest looking into your build for any
requirecalls.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
requirein ESM like this:Same error when using ES6 in a GitHub action based on https://github.com/actions/javascript-action @vercel/ncc version 0.36.1
Any solution?
This issue still happens, no official fixes yet?