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

Support sloppy mode #3119

Closed
pdenapo opened this issue May 30, 2023 · 5 comments
Closed

Support sloppy mode #3119

pdenapo opened this issue May 30, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@pdenapo
Copy link

pdenapo commented May 30, 2023

What version of Bun is running?

0.6.6

What platform is your computer?

Linux 5.15.0-40-generic x86_64 x86_64

What steps can reproduce the bug?

Run the following test.js program

a=2
console.log(a)

What is the expected behavior?

In node.js (version 16.20.0) the output is

2

What do you see instead?

In bun version 0.6.5 this gives an error

ReferenceError: Can't find variable: a

Additional information

While perhaps the current Bun behavior could help writing better code, this is clearly an incompatibility with node that could potentially break a lot of existing code.

I would suggest an option like "bun --declare-vars test.js" (or in a config file) in case that declaring all the variables with let is required.

@pdenapo pdenapo added the bug Something isn't working label May 30, 2023
@paperdave
Copy link
Collaborator

bun loads code all code in strict mode by default, which is intentional (es modules define this as spec, and node esm does this too). strict mode doesn't allow a number of things like undeclared variables.

I wonder if there's a nice way we can enable sloppy mode.

@Jarred-Sumner
Copy link
Collaborator

Probably the better fix here is to detect usages of sloppy mode features and build as CommonJS in that case

@pdenapo
Copy link
Author

pdenapo commented May 30, 2023

It seems that the usual way of declaring that you want to use strict mode is adding "use strict"; to the beginning of a script or a function.

https://www.w3schools.com/js/js_strict.asp

I've checked that node.js supports this!

@simylein
Copy link
Contributor

I kinda like the idea that strict mode is opt out and not opt in because it helps building more modern projects.

@Electroid Electroid changed the title Bun requieres to use let for variable assignment, Node does not. Support sloppy mode Feb 5, 2024
@Jarred-Sumner
Copy link
Collaborator

Probably the better fix here is to detect usages of sloppy mode features and build as CommonJS in that case

This has since been implemented

To enable sloppy mode, use CommonJS in your module and don't include "use strict" at the top. Or, make the file extension .cjs

We're unlikely to change this at this stage tbh

@Jarred-Sumner Jarred-Sumner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
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

4 participants