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

"process.env.NODE_ENV" is defined as an identifier instead of a string #583

Closed
jjenzz opened this issue Dec 7, 2020 · 7 comments
Closed

Comments

@jjenzz
Copy link

jjenzz commented Dec 7, 2020

I'm trying to use esbuild on a mac with the following script:

"build:dev": "esbuild index.ts --bundle --define:process.env.NODE_ENV=\"development\" --minify --sourcemap --platform=browser --outfile=dist/index.development.js"

But I always get the following warning:

> warning: "process.env.NODE_ENV" is defined as an identifier instead of a string (surround "development" with double quotes to get a string)

I've tried single quotes without luck also. Is this warning being incorrectly reported or am I doing something wrong here?

@nettybun
Copy link

nettybun commented Dec 7, 2020

It's in #466

You need to use '--define:process.env.NODE_ENV="production"' not --define:process.env.NODE_ENV="production" because your shell eats the outer quotes.

Shells are messy. I think it's good you opened this issue so there's now something people can search for

@evanw
Copy link
Owner

evanw commented Dec 7, 2020

The answer is in the documentation for the define feature: https://esbuild.github.io/api/#define. Your shell is removing the outer layer of quotes. That means by the time esbuild sees it, it’s as if you never had any quotes in the first place. This is why it gives you this warning.

While it’s true that using two layers of quotes works for bash (since bash will only remove the outer layer of quotes), this isn’t portable to Windows command prompt. The portable approach is to escape the quotes in the shell command using a backslash like this: \". Since you are encoding the shell command in a JSON string, you need to escape each of those characters again for JSON like this: \\\". The final build command would look like this:

"build:dev": "esbuild index.ts --bundle --define:process.env.NODE_ENV=\\\"development\\\" --minify --sourcemap --platform=browser --outfile=dist/index.development.js"

@jjenzz
Copy link
Author

jjenzz commented Dec 8, 2020

Ooooh, I had tried to double them but hadn't tried tripling them. admittedly only looked as far as the "bundling for browser" docs 🙈 Thank youuuuu!!

@evanw
Copy link
Owner

evanw commented Dec 8, 2020

No worries. I'm going to close this issue since it sounds like it has been resolved.

@evanw evanw closed this as completed Dec 8, 2020
@mattrossman
Copy link

For anyone else wondering how to do this in the JS API, this appears to work:

require('esbuild').buildSync({
  define: { 'process.env.NODE_ENV': '"production"' },
})

I intially expected that I'd have to turn the process.env.NODE_ENV part into a nested object of some sort

@i-priyanshu
Copy link

i-priyanshu commented May 25, 2021

I have tried using single as well triple quote , yet still the terminal is generating error.

I am currently using Windows 10 and terminal i am using is bash

The command i am trying to run is:
"prepublishOnly": "esbuild src/index.ts --platform=node --outfile=dist/index.js --bundle --minify --define:process.env.NODE_ENV = \\\"production\\\""

I have even tried using single slash as well..but it still didin't work

--define:process.env.NODE_ENV = \\\"production\\\""

Error Output for triple quote statement is somewhat like this.
--define:process.env.NODE_ENV = \production\

As for single quote statement
--define:process.env.NODE_ENV = production

It would be a great help if anyone could solve it..i have been stuck here for a while now

@evanw
Copy link
Owner

evanw commented May 25, 2021

One potential way to fix this is to just remove the --define flag completely as it's no longer necessary. Since version 0.11.3, esbuild now automatically sets process.env.NODE_ENV to "development" when not minifying and "production" otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants