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 replace bug #13597

Closed
7 tasks done
ankyo opened this issue Jun 22, 2023 · 7 comments
Closed
7 tasks done

process.env.NODE_ENV replace bug #13597

ankyo opened this issue Jun 22, 2023 · 7 comments

Comments

@ankyo
Copy link

ankyo commented Jun 22, 2023

Describe the bug

Error: Uncaught SyntaxError: Unexpected string (at msw.js?v=12ee2438:6178:115)

graphql/jsutils/instanceOf.js

_globalThis$process.env.NODE_ENV) === 'production'

.vite/deps/msw.js in browser

_globalThis$"development") === "production"

Reproduction

No Link

Steps to reproduce

The above should make the problem clear

System Info

System independent

Used Package Manager

pnpm

Logs

No response

Validations

@ankyo
Copy link
Author

ankyo commented Jun 22, 2023

vite 4.3.9

@skeezrxcco
Copy link

skeezrxcco commented Jun 22, 2023

We're having the same issue as well in our end, after updating npm.

@ankyo were you able to resolve it ?

@ankyo
Copy link
Author

ankyo commented Jun 22, 2023

We're having the same issue as well in our end, after updating npm.

@ankyo were you able to resolve it ?

You can write a simple plugin to fix it

import { createUnplugin } from 'unplugin'

export const unplugin = createUnplugin((mode: string) => {
  return {
    name: 'unplugin-fixenv',
    transform(code) {
      if (code.includes('process.env.NODE_ENV')) {
        return code.replace(/_globalThis\$process\.env\.NODE_ENV/, `"${mode}"`)
      }
      return code
    },
  }
})

export const fixEnvForVite = unplugin.vite
export default defineConfig(
  {
    mode: 'development',
    plugins: [
      fixEnvForVite('development'),
    ],
  },
)

it works for me

@skeezrxcco
Copy link

Thanks a lot it worked! btw @ankyo do you think we're going to see an 'internal' fix that pertains to this issue or does this plugin makes the cut on the long term?

@github-actions
Copy link

Hello @ankyo. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@auvred
Copy link

auvred commented Jun 24, 2023

Hi, this is a known issue, it seems that the fix will be shipped in v5.0 (#11151)

The issue occurs because $ treats as word boundary (\b) in this regular expression

const process_env_NODE_ENV_RE =
/(\bglobal(This)?\.)?\bprocess\.env\.NODE_ENV\b/g

@bluwy
Copy link
Member

bluwy commented Jun 25, 2023

Yeah I think this is a duplicate of #9829. Closing in favor of it.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants