ESM module for both Node.js and the Browser?
See original GitHub issueDescribe the bug
The httpie
package specifies “browser” paths on its package.json
multiple times, but frontend build tools don’t seem to pick up the correct file (always selecting node/index.mjs
)
ERROR
[vite] Dep optimization failed with error:
Could not load http (imported by node_modules/httpie/node/index.mjs): ENOENT: no such file or directory, open 'http'
[Error: Could not load http (imported by node_modules/httpie/node/index.mjs): ENOENT: no such file or directory, open 'http'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'http',
watchFiles: [
'/Users/endel/projects/vite-esm-browser-node/node_modules/httpie/node/index.mjs',
'/Users/endel/projects/vite-esm-browser-node/node_modules/vue/dist/vue.runtime.esm-bundler.js',
'/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js',
'/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/shared/dist/shared.esm-bundler.js',
'/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js',
'/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js',
'http',
'https',
'url'
]
}
Reproduction
I’ve created a repository describing this problem https://github.com/endel/esm-browser-node
In fact, this problem is present on every ESM JavaScript bundler, I’ve found it very difficult to search about this on the web and I appreciate if you could point me in the right direction in case there is some discussion already going on about this! 🙏
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Using ECMAScript modules (ESM) with Node.js
Learn about using ES modules in Node.js today and get a closer look at how you can migrate your codebase to make use...
Read more >How to use ESM on the web and in Node.js - ByteofDev
ESM (or ECMAScript Modules) is a modern module format with many advantages over previous formats like CommonJS. It is supported natively in most ......
Read more >ES Modules in NodeJS and the Browser - YouTube
This tutorial explains how you can use ES Modules in either NodeJS or in the Browser along with some of the gotchas and...
Read more >ECMAScript modules | Node.js v19.3.0 Documentation
Node.js has two module systems: CommonJS modules and ECMAScript modules. Authors can tell Node.js to use the ECMAScript modules loader via the .mjs...
Read more >How to write ESM modules that work in both the browser, and ...
json that set-up whatever environment-specific elements before importing and re-exporting the main module. //main.js fix for nodejs import { ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Please read the docs https://vitejs.dev/config/#alias
It still works for
httpie
because thebrowser
field inexports
is still prioritized. Technically,module
is not a Node.js concept and should be treated as browser only.