bun
bun copied to clipboard
Assigning process.env.NODE_ENV -> "SyntaxError: Left side of assignment is not a reference"
Version
0.1.2
Platform
Linux JDESK 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux ---- Microsoft Windows NT 10.0.19043.0 x64
What steps will reproduce the bug?
Attempt to directly assign a string to process.env.NODE_ENV in any JS or TS script.
process.env.FOO = 'test1'; // works without errors
process.env.NODE_ENV = 'test2'; // SyntaxError: Left side of assignment is not a reference.
How often does it reproduce? Is there a required condition?
Consistently
What is the expected behavior?
Expected behavior: A string is assigned to process.env.NODE_ENV just like any other assignment to process.env.X.
What do you see instead?
SyntaxError: Left side of assignment is not a reference.
at /path/to/test.js:3:0
Additional information
No response
This happens because process.env.NODE_ENV doesn't exist at runtime. This enables dead code elimination
This:
process.env.NODE_ENV = 'test1';
Becomes:
"development" = 'test1';
To set process.env.NODE_ENV, use --define like so:
bun run --define:process.env.NODE_ENV="'production'" script.js
Perhaps there should be a better error or a runtime-only version should be set. I'm not exactly sure what to do, but code using this shouldn't break.
This happens because
process.env.NODE_ENVdoesn't exist at runtime. This enables dead code elimination
Is that in a spec somewhere or was it decided by you and/or other Bun developers?
NodeJS does allow NODE_ENV to be overwritten at runtime:
~$ cat test.js
console.log(`process.env.NODE_ENV = ${process.env.NODE_ENV}`);
require('./test1');
console.log(`process.env.NODE_ENV = ${process.env.NODE_ENV}`);
~$ cat test1.js
process.env.NODE_ENV = 'test1';
~$ node test.js
process.env.NODE_ENV = undefined
process.env.NODE_ENV = test1
It breaks jest

bun --define process.env.NODE_ENV:"test" path/to/jest also does not work
SegmentationFault at 5680500659196
–––– bun meta ––––
Bun v0.1.3 macOS x64 19.4.0
AutoCommand: define
Elapsed: 43ms | User: 28ms | Sys: 13ms
RSS: 20.94MB | Peak: 20.94MB | Commit: 67.11MB | Faults: 0
–––– bun meta ––––
Ask for #help in https://bun.sh/discord or go to https://bun.sh/issues
Same is true for process.env.PATH. The GitHub Actions toolkit sets this here so currently it isn't possible to use this toolkit to create JS GitHub Actions powered by bun.
Edit: This might actually be related to something else, e.g. child-process not yet supported.
Seems to affect ShellJS as well.
Test code:
import * as shell from 'shelljs';
shell.echo('hello');
Error:
20 | // Include the docs for all the default commands
21 | //@commands
22 |
23 | // Load all default commands
24 | require('./commands').forEach(function (command) {
25 | require('./src/' + command);
^
TypeError: Left side of assignment is not a reference. while parsing module "/tmp/node_modules/shelljs/src/cd.js"
at /tmp/node_modules/shelljs/shell.js:25:2
at /tmp/node_modules/shelljs/shell.js:24:8
at bun:wrap:1:16360
The snippet of the code using process.env from node_modules/shelljs/src/cd.js:
//@ Changes to directory `dir` for the duration of the script. Changes to home
//@ directory if no argument is supplied.
function _cd(options, dir) {
if (!dir) dir = os.homedir();
if (dir === '-') {
if (!process.env.OLDPWD) {
common.error('could not find previous directory');
} else {
dir = process.env.OLDPWD;
}
}
Just want to confirm, this is a compatibility issue that we will fix.
@robobun
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';
@Electroid here you go!
SyntaxError: Left side of assignment is not a reference.
at /tmp/bun-uwqFLw/index.js:3:0
Code
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';
Ran using the latest build of Bun, an all-in-one JavaScript runtime.
As of bun version 0.7.1 this will no longer throw a SyntaxError. However, modifying process.env.NODE_ENV will not stick, is this intentional behaviour?
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';
console.log(process.env.FOO); // logs test1
console.log(process.env.NODE_ENV); // logs development
As of bun version 0.7.1 this will no longer throw a
SyntaxError. However, modifyingprocess.env.NODE_ENVwill not stick, is this intentional behaviour?process.env.FOO = 'test1'; process.env.NODE_ENV = 'test2'; console.log(process.env.FOO); // logs test1 console.log(process.env.NODE_ENV); // logs development
Looks like process.env.NODE_ENV still gets replaced with the string "development" when it's syntactically valid, i.e. not the left side of an assignment:
$ bun build test.js
// test.js
process.env.FOO = "test1";
process.env.NODE_ENV = "test2";
console.log(process.env.FOO);
console.log("development");
yeah we talked about this here https://github.com/oven-sh/bun/issues/3835
This has been fixed, tested using Bun v1.0.7. If you still run into this error, please feel free to re-open this issue.
Hi @Electroid, I just upgraded bun and tested the same test.js file as in the previous comment, and unfortunately the behavior is the same.
drak@JDESK:~$ cat test.js
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';
console.log(process.env.FOO); // logs test1
console.log(process.env.NODE_ENV); // logs development
drak@JDESK:~$ bun -v
1.0.7
drak@JDESK:~$ bun test.js
test1
development
drak@JDESK:~$ bun build test.js --target=bun
// @bun
// test.js
process.env.FOO = "test1";
process.env.NODE_ENV = "test2";
console.log(process.env.FOO);
console.log("development");
drak@JDESK:~$
FYI, as a non contributor I don't think I have permission to re-open issues.
Thanks!
We removed the inlining feature in the runtime, so this bug no longer occurs.
Do we know how big the performance implications of not inlining anymore are?