yargs: bundle with esbuild fails because `yargs/yargs` does not have file extension
Possible duplicate of #1929 but Iām not sure because that ticket is too vague.
esbuild determines content types based on file extension. esbuild docs
But yargs/yargs does not have a file extension.
This raises an error:
$ echo 'import createYargs from "yargs/yargs";' | yarn esbuild --bundle --loader=js
> <stdin>:1:24: error: Do not know how to load path: node_modules/yargs/yargs
1 ā import createYargs from "yargs/yargs";
āµ ~~~~~~~~~~~~~
1 error
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 15 (8 by maintainers)
This is becoming a fatal error with Node 17.x where the top-level project is Node.js ESM (not TypeScript).
Hello! I am here to share my solution. You can get around this error by using the following esbuild plugin: esbuild-plugin-alias With the plugin, you can alias some file call, like this:
Keeping this file without an extension is a hack necessary to prevent yargs from treating the file as ESM, since for the benefit of TypeScript, the package.json is of
type,module.This can be fixed as soon as TypeScript supports a
.mjsextension.@cspotcode no, this design grew organically while trying to support both CJS and ESM in the same module.
The approach is discussed, without much detail, here.
Open to other approaches as long as all existing tests continue working, and we verify functionality on all supported Node runtimes.