{"id":18084572,"url":"https://github.com/endel/esm-browser-node","last_synced_at":"2025-07-21T04:08:42.099Z","repository":{"id":137832698,"uuid":"316317943","full_name":"endel/esm-browser-node","owner":"endel","description":"Are frontend build tools compatible with Node.js/Browser targets?","archived":false,"fork":false,"pushed_at":"2020-11-27T03:02:30.000Z","size":222,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-13T10:09:37.241Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/endel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-26T19:15:02.000Z","updated_at":"2024-06-14T05:14:54.000Z","dependencies_parsed_at":"2023-04-17T05:05:02.533Z","dependency_job_id":null,"html_url":"https://github.com/endel/esm-browser-node","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/endel/esm-browser-node","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endel%2Fesm-browser-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endel%2Fesm-browser-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endel%2Fesm-browser-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endel%2Fesm-browser-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/endel","download_url":"https://codeload.github.com/endel/esm-browser-node/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endel%2Fesm-browser-node/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266236999,"owners_count":23897308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-31T15:07:13.562Z","updated_at":"2025-07-21T04:08:42.084Z","avatar_url":"https://github.com/endel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Broken ESM modules?\n\n\u003e Is it possible to have an ESM module that works both on Node.js and in the browser??\n\nEXAMPLE: 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` (and failing because it has a built-in node dependency).\n\n\u003cimg src=\"screenshot.png?raw=1\" /\u003e\n\n**Expectation:** When bundling a dependency that has both `\"module\"` and `\"browser\"` field, it is expected that the build-tool is going to use the **`\"browser\"`** version of it, since the **`\"module\"`** version is potentially targetted for Node.js (thus having a Node.js dependency inside)\n\n**Reality:** Most build tools are trying to get the `\"module\"` version, and fail because the `\"module\"` version was made for Node.js and not browsers.\n\n# Build tool\n\n- [vite](#using-vite) ❌\n- [snowpack](#using-snowpack) ❌\n- [webpack](#using-webpack) ❌\n- [esbuild](#using-esbuild) ✅\n- [rollup](#using-rollup) ❌\n\n## How to reproduce\n\n## Using `vite`\n\n```\ncd vite\nnpm install\nnpm run dev\n```\n\n**file included:** `node/index.mjs` ❌\n\n```\n[vite] Dep optimization failed with error:\nCould not load http (imported by node_modules/httpie/node/index.mjs): ENOENT: no such file or directory, open 'http'\n[Error: Could not load http (imported by node_modules/httpie/node/index.mjs): ENOENT: no such file or directory, open 'http'] {\n  errno: -2,\n  code: 'ENOENT',\n  syscall: 'open',\n  path: 'http',\n  watchFiles: [\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/httpie/node/index.mjs',\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/vue/dist/vue.runtime.esm-bundler.js',\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js',\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/shared/dist/shared.esm-bundler.js',\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js',\n    '/Users/endel/projects/vite-esm-browser-node/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js',\n    'http',\n    'https',\n    'url'\n  ]\n}\n```\n\n## Using `snowpack`\n\n```\ncd snowpack\nnpm install\nnpm start\n```\n\n**file included:** `node/index.mjs` ❌\n\n```\n[snowpack] node_modules/httpie/node/index.mjs\n   Module \"https\" (Node.js built-in) is not available in the browser. Run Snowpack with --polyfill-node to fix.\n```\n\n\n## Using `webpack`\n\n```\ncd webpack\nnpm install\nnpm start\n```\n\n**file included:** `node/index.mjs` ❌\n\n```\nERROR in ./node_modules/httpie/node/index.mjs 1:0-32\nModule not found: Error: Can't resolve 'https' in '/Users/endel/projects/esm-browser-node/webpack/node_modules/httpie/node'\n```\n\n\n## Using `esbuild`\n\n```\ncd esbuild\nnpm install\nnpm start\n```\n\n`esbuild` successfully uses the \"/xhr/index.mjs\" path!\n\n**file included:** `xhr/index.mjs` ✅\n\nThough, its [comment section](https://github.com/evanw/esbuild/blob/f4cec94deaa61e5bb9bd3c0d14ad37ead1d8ca55/internal/resolver/resolver.go#L26-L33) references to an unresolved webpack issue ([webpack/webpack#4674](https://github.com/webpack/webpack/issues/4674)) as the guidance for the implementation - that has changed direction already. esbuild might then eventually move towards the latest webpack implementation and fail on this aspect as others do.\n\n## Using `rollup`\n\n```\ncd rollup\nnpm install\nnpm start\n```\n\n**file included:** `node/index.mjs` ❌\n\n```\n(!) Plugin node-resolve: preferring built-in module 'https' over local alternative at 'https', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning\n(!) Plugin node-resolve: preferring built-in module 'http' over local alternative at 'http', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning\n(!) Plugin node-resolve: preferring built-in module 'url' over local alternative at 'url', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning\n(!) Unresolved dependencies\nhttps://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency\nhttps (imported by node_modules/httpie/node/index.mjs)\nhttp (imported by node_modules/httpie/node/index.mjs)\nurl (imported by node_modules/httpie/node/index.mjs)\ncreated output in 223ms\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendel%2Fesm-browser-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fendel%2Fesm-browser-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendel%2Fesm-browser-node/lists"}