`require.main === module` true when it should be false in node bundle
- 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