evanw / evanw/esbuild

`require.main === module` true when it should be false in node bundle

Open
#2,121 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Would be nice if there was a convenient toggle to make the standard `required.main === module` test work as expected. Currently, it ends up as true everywhere. Here's an example:

```ts
// a.ts
export const five: number = 5
if (require.main === module) console.log({ five })

// b.ts
import { five } from './a'
const twenty: number = five * 4
if (require.main === module) console.log({ twenty })
```

If you bundle and run `b.ts` then it prints five and twenty, instead of just five. This is different from ts-node's behavior

Reproduction: https://github.com/qpwo/esbuild-issue-2121

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked qpwo/esbuild-issue-2121 reproduction and compare the bundled behavior when running b.ts with the ts-node behavior described in the issue. Done means a documented toggle or configuration makes require.main === module distinguish the entry module from imported modules without printing both five and twenty.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
build-system, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.