evanw / evanw/esbuild

align require(esm) behavior with node

Open
#4,102 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

I tested some require(esm) edge cases at https://github.com/easrng/require-esm-tests. Here's where esbuild is differing from node:
- each call to `require` for a module is returning a new CommonJS wrapper (ex. `require('mod') !== require('mod')` when mod is esm in esbuild)
- esbuild always defines `__esModule` as `{ value: true, writable: false, enumerable: false, configurable: false}`, node defines it as `{ value: true, writable: true, enumerable: true, configurable: true}` (you probably want to keep using writable: false for consistency with other exports which are getter-only?) and it only defines it if there's no export named `__esModule` and the module has a default export
- when `__esModule` doesn't need to be added `require("mod") === await import("mod")` should be true
- esbuild does not support `export { x as "module.exports" }`, which sets `require`'s return value directly in node

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the cases in https://github.com/easrng/require-esm-tests and compare esbuild's require(esm) results with Node. Investigate wrapper identity, __esModule descriptors and conditions, and export { x as "module.exports"}; done means the listed edge cases match Node's behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript, node.js
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.