-
Notifications
You must be signed in to change notification settings - Fork 287
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
"require is not defined in ES module scope" and mixed require/import using ESM with native modules #791
Comments
having the same issue when trying to bundle a ReferenceError: require is not defined in ES module scope, you can use import instead
at eval (eval at 874 (file:///Users/lex/Projects/FLDV/FLDV-P008/node_modules/@flex-development/trext/src/index.ts:7:15), <anonymous>:1:1)
at Object.874 (file:///Users/lex/Projects/FLDV/FLDV-P008/node_modules/@flex-development/trext/src/index.ts:7:15)
at __nccwpck_require__ (file:///Users/lex/Projects/FLDV/FLDV-P008/node_modules/@flex-development/trext/esm/trext.mjs:57:41)
at file:///Users/lex/Projects/FLDV/FLDV-P008/node_modules/@flex-development/trext/esm/trext.mjs:100:81
at file:///Users/lex/Projects/FLDV/FLDV-P008/node_modules/@flex-development/trext/esm/trext.mjs:113:3
at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5) /******/ // Execute the module function
/******/ var threw = true;
/******/ try {
/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
/******/ threw = false;
/******/ } finally {
/******/ if(threw) delete __webpack_module_cache__[moduleId];
/******/ } in my case, |
For my use case, I was using the 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 |
same issue |
+1 - I believe I'm encountering this same issue - chrisreddington/rss-parser#120 |
I have a similar problemIt seems that not all Fix
Example// Before replacement
module.exports = eval("require")("@aws-sdk/signature-v4-crt");
module.exports = require("assert");
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("http2");
// After replacement
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("@aws-sdk/signature-v4-crt");
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("assert");
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("http2");
// After minification
e=>{e.exports=(0,o.createRequire)(import.meta.url)("@aws-sdk/signature-v4-crt")};
e=>{e.exports=(0,o.createRequire)(import.meta.url)("assert")};
e=>{e.exports=(0,o.createRequire)(import.meta.url)("http2")}; Comment: It looks like a very bad trick, but it works for me. You can try to automate this.My package.json {
"main": "build/index.js",
"type": "module",
"scripts": {
"start": "ts-node src/index.ts",
"build": "ncc build src/index.ts -o build -m --license licenses.txt"
},
"dependencies": {
"@actions/core": "1.10.0",
"@aws-sdk/client-s3": "3.289.0",
"mime-types": "2.1.35"
},
"devDependencies": {
"@types/mime-types": "2.1.1",
"@types/node": "18.15.0",
"@vercel/ncc": "0.36.1",
"ts-node": "10.9.1",
"typescript": "4.9.5"
}
} tsconfig.json {
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ESNext"],
"types": ["node"],
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"build",
"node_modules"
],
"ts-node": {
"esm": true
}
} |
Same error when using ES6 in a GitHub action based on https://github.com/actions/javascript-action
Any solution? |
|
I guess you mean:
I'm using "@vercel/ncc@lastest" as well (0.36.1 right now) , with node 16.20.1 |
This comment was marked as spam.
This comment was marked as spam.
This issue still happens, no official fixes yet? |
Seems like I was able to apply a workaround for this problem: polyfilling // 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 |
I've tried your workaround but it didn't work for me unfortunately I still get
|
This helped me (gnu sed in OSX "is" gsed). gsed -i 's/module=>{module.exports=eval("require")("")}/module=>{module.exports={}}/g' index.js |
I found this piece of code in my build:
This happened because I suggest looking into your build for any Either way NCC should not output such |
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
changing it to import { App } from `./App.js`; fixed the problem :/ |
I'm trying to compile an ES module (
"type": "module"
) that uses tree-sitter. The compiled distributable winds upimport
ing createRequire as expected, but still tries to load native code via old-fashionedrequire
.package.json
index.js
The offending
require
in question:The text was updated successfully, but these errors were encountered: