-
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
esbuild + ESM + NodeJS + Typescript troubles #3637
Comments
First of all let's explain the error:
It is thrown by esbuild's var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof
Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined")
return require.apply(this, arguments);
+ throw Error('Dynamic require of "' + x + '" is not supported');
}); So which code causes this error? You can find the call-chain:
You can find more details in #1921. There are some solutions but none of them is the silver bullet.
|
Appreciate the quick response. I'm new to the JS and Node, so the plethora of module formats and versions is a bit baffling. Looking at the actual Could esbuild pull in the mjs file even though the parent used Its also unclear to me why esbuild without |
Yep. If you looking at the node-fetch's package.json, the However that won't make your example above work because node-fetch depends on whatwg-url which is a CJS only package, the
This was because esbuild recursively resolves source files and it will shutdown when it hit any errors. Your |
But if I delete all use of |
Sorry I didn't understand the question. You're asking why this command works:
When there's no --platform=node, the default platform is browser which means that esbuild would try to produce a bundle that can run in the browser (which doesn't mean that it cannot run in Node.js, as long as the features it used is compatible in these environments). The resolver would prefer browser specific fields in package.json (browser > module > main). The result is |
Thanks for all the detail. I hope this helps other newbies |
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment)
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment)
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment)
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment) This solution still uses external node_modules.
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment) This solution still uses external node_modules.
Trying to get the combo in the title of this ticket working and have not found the correct incantation. I found a few posts saying it won't work, so decided to report a bug / feature request / post to understand if this should work.
Setup
Simple server app that will run in nodejs v20.x in an AWS lambda function
Would like to use ESM style modules
Using just '@simplewebauthn/server' package and native node
Ubuntu 22.04 in a VM on MacBook Pro
I have tried various config options, this was the latest attempt:
package.json
tsconfig.json
index.ts
Results
If I build with the following:
./node_modules/.bin/esbuild --bundle --format=esm --target=es2022 --outdir=build src/index.ts
I get the error below, which is what I'd expect. If I delete the nodejs native
fs
usage, the project builds and runs without error (but I need to be able import nodejs modules). Also, withfs
removed if I edit the outputindex.js
file there are no require statements.As described in esbuild docs, I add the --platform=node option as follows:
./node_modules/.bin/esbuild --bundle --format=esm --platform=node --target=es2022 --outdir=build src/index.ts
The project builds but when I run it with
nodejs build/index.js
I get an error like the following with a stack trace. When I edit the output
index.js
file I find many require statements.Is there a way to use esbuild for a typescript project with ESM modules and nodejs?
The text was updated successfully, but these errors were encountered: