question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESM module for both Node.js and the Browser?

See original GitHub issue

Describe 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)

screenshot

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:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
yyx990803commented, Jan 5, 2021

Please read the docs https://vitejs.dev/config/#alias

1reaction
yyx990803commented, Jan 2, 2021

It still works for httpie because the browser field in exports is still prioritized. Technically, module is not a Node.js concept and should be treated as browser only.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found