Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require.main !== module (node compat) #2107

Closed
AgentEnder opened this issue Feb 19, 2023 · 3 comments
Closed

require.main !== module (node compat) #2107

AgentEnder opened this issue Feb 19, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@AgentEnder
Copy link
Contributor

What version of Bun is running?

0.5.6

What platform is your computer?

Linux 5.10.16.3-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Try to run this script w/ bun:

if (require.main === module) {
	console.log('Running as main')
} else {
    console.log('required by other module')
}

What is the expected behavior?

The first statement is logged

What do you see instead?

The second statement is logged

Additional information

This was found when testing compatibility with Nx encapsulated installs, as an experiment to see if bun could run Nx well. So far, this and #401 are two "blockers" that I've found.

@AgentEnder AgentEnder added the bug Something isn't working label Feb 19, 2023
@Jarred-Sumner
Copy link
Collaborator

In the current implementation of require, this wouldn't work (CJS modules share the same runtime code as in browsers, which would be one of the things that need to change to support this)

@AgentEnder
Copy link
Contributor Author

Could it be something that is changed when transpiling to the web-compatible format?

In that format would import.meta.url work?

E.g. https://stackoverflow.com/questions/57838022/detect-whether-es-module-is-run-from-command-line-in-node

@birkskyum
Copy link
Collaborator

Working now:

➜ bun --bun index.ts
Running as main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants