-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ReferenceError: require is not defined is ESM Node projects #946
Comments
I ran into something similar to this when playing with I don’t know how relevant this is for you but given I encountered the same error, I needed to do this: So my guess is what Edit: My point is that I don’t think this has anything to do w/ esbuild. I think this has to do with evaluating JavaScript programmatically / in-memory. |
@zaydek Thank you for the additional information… interesting. If that is the case, it doesn’t affect good ’ol Update: here’s a link to the relevant code in Node, in case it helps: https://github.com/nodejs/node/blob/831f4c755d2270e7b44c075176ab4a05698d21d9/lib/internal/modules/cjs/helpers.js#L48 Given that it’s a single line of code that needs to be added, I’m just going to inject that as part of my build process for the time being. |
My hacky workaround for the moment is running this after the esbuild script in the npm build task (the app is a CLI app, hence the hashbang at the start: import fs from 'fs'
import path from 'path'
const __dirname = new URL('.', import.meta.url).pathname
const build = path.join(__dirname, '..', 'place.js')
const unpatchedBuild = fs.readFileSync(build, 'utf-8')
const patchedBuild = unpatchedBuild.replace('#!/usr/bin/env node', '#!/usr/bin/env node\n\nconst require = createRequire(import.meta.url)\n')
fs.writeFileSync(build, patchedBuild, 'utf-8') |
In case anyone else gets tripped up by this error when converting cjs to esm (where esbuild.build({
banner: {
js: "import { createRequire as topLevelCreateRequire } from 'module';\n const require = topLevelCreateRequire(import.meta.url);"
},
...
}) Note: it is a good idea to rename |
@popeindustries That’s excellent, thank you :) |
@popeindustries amazing, thanks! This can also be passed in on the command line: esbuild index.ts --bundle --outfile=out.mjs --platform=node --target=node16.8 --format=esm --banner:js='import { createRequire as topLevelCreateRequire } from \"module\"; const require = topLevelCreateRequire(import.meta.url);' |
I don't think it's related to the JS engine. The side effect of ESM modules is that Since Unfortunately, this does not explain the problem with esbuild. Why isn't We should take a better look at the output. |
Just a heads up that the build was choking on the |
Thanks, I've edited my comment above. I think the |
@karlhorky Thanks, Karl. (In my case, it was failing on Linux.) |
This might be the answer for many, especially for those using ESM with AWS SDK v3. aws/aws-sdk-js-v3#4217 (comment) bundling: {
format: aws_lambda_nodejs.OutputFormat.ESM,
mainFields: ["module", "main"],
},
architecture: aws_lambda.Architecture.ARM_64, |
@evanw It would be amazing if we could have a solution to the current errors that didn't require including a banner to separately define |
When I just added `--format=esm`, I saw runtime errors about `require` not being defined. I used a trick from evanw/esbuild#946 (comment) that seems to compile and execute happily.
## [1.4.1](v1.4.0...v1.4.1) (2023-11-12) ### Bug Fixes * bundle as an ES Module ([7561499](7561499)), closes [/github.com/evanw/esbuild/issues/946#issuecomment-911869872](https://github.com//github.com/evanw/esbuild/issues/946/issues/issuecomment-911869872)
In my ESM Node project, Place that also imports CommonJS modules (any any Node project these days probably does given ESM modules are not the norm yet), I get the following error when I run the bundle:
If I add the following line to the top of the bundle to create the require function, it runs:
This is not a small, reproducible case but filing it anyway in case it helps without one.
To reproduce
npm i
npm run build
Try to run the generated
place.js
with, e.g.,./place.js evanw.small-web.org
and you should see it fail with the error above. If you add the line mentioned above, it should run.The text was updated successfully, but these errors were encountered: