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

Error when using [email protected] "Expected value for define "process.env.NODE_ENV" to be a string" #205

Open
pkerschbaum opened this issue Jan 12, 2023 · 4 comments

Comments

@pkerschbaum
Copy link

pkerschbaum commented Jan 12, 2023

  • mdx-bundler version: 9.2.1
  • node version: 16.14.2
  • npm version: 7.17.0

Relevant code or config
The example code from https://github.com/kentcdodds/mdx-bundler#usage.

What you did:
Installed mdx-bundler and esbuild and ran the example code.

What happened:

❯ node .
✘ [ERROR] Expected value for define "process.env.NODE_ENV" to be a string, got undefined instead

Error: Build failed with 1 error:
error: Expected value for define "process.env.NODE_ENV" to be a string, got undefined instead
    at async ESMLoader.import (https://nodesyqie8-xvjd.w-credentialless.staticblitz.com/blitz.6f912e90fcb88a8621a295889b82f338c77531e6.js:6:1209052)
    at async i.loadESM (https://nodesyqie8-xvjd.w-credentialless.staticblitz.com/blitz.6f912e90fcb88a8621a295889b82f338c77531e6.js:6:246622)
    at async handleMainPromise (https://nodesyqie8-xvjd.w-credentialless.staticblitz.com/blitz.6f912e90fcb88a8621a295889b82f338c77531e6.js:6:989060) {
  errors: [
    {
      id: '',
      pluginName: '',
      text: 'Expected value for define "process.env.NODE_ENV" to be a string, got undefined instead',
      location: null,
      notes: [],
      detail: Error: Expected value for define "process.env.NODE_ENV" to be a string, got undefined instead
          at validateStringValue (file:///home/projects/node-syqie8/node_modules/esbuild/lib/main.js:290:11)
          at pushCommonFlags (file:///home/projects/node-syqie8/node_modules/esbuild/lib/main.js:381:37)
          at flagsForBuildOptions (file:///home/projects/node-syqie8/node_modules/esbuild/lib/main.js:416:3)
          at buildOrServeContinue (file:///home/projects/node-syqie8/node_modules/esbuild/lib/main.js:1008:9)
          at eval (file:///home/projects/node-syqie8/node_modules/esbuild/lib/main.js:981:11)
    }
  ],
  warnings: []
}

Reproduction repository:

https://stackblitz.com/edit/node-syqie8

Problem description:
mdx-bundler defines 'process.env.NODE_ENV' here, via JSON.stringify:

'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
.
If the value undefined is passed to JSON.stringify, it will return the value undefined (see mdn web docs).
But esbuild expects a string instead. This was introduced in v0.16.0, see section "Add additional validation of API parameters" of https://github.com/evanw/esbuild/releases/tag/v0.16.0.

Suggested solution:
Define process.env.NODE_ENV only if it is set.

pkerschbaum added a commit to pkerschbaum/pkerschbaum-homepage that referenced this issue Feb 3, 2023
pkerschbaum added a commit to pkerschbaum/pkerschbaum-homepage that referenced this issue Feb 3, 2023
* refactor(web): migrate to moduleResolution node16

also revert cec630d because "imports" is not supported well enough yet

* chore(deps): ignore updates for "esbuild"

because of kentcdodds/mdx-bundler#205
@danpacho
Copy link

In my case, this problem is fixed via installing esbuild version 0.13.15.

@Toshinaki
Copy link

I'm facing the same issue here.
Why the fix is not merged?

@riley-worthington
Copy link

I had this same issue in a Remix application on mdx-bundler 10.0.1 and esbuild 0.20.1. I was able to fix it by adding this esbuildOptions parameter to bundleMDX:

esbuildOptions(options) {
  options.define = {
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  };
  return options;
}

@CanRau
Copy link
Contributor

CanRau commented Mar 12, 2024

I had this same issue in a Remix application on mdx-bundler 10.0.1 and esbuild 0.20.1. I was able to fix it by adding this esbuildOptions parameter to bundleMDX:

esbuildOptions(options) {
  options.define = {
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  };
  return options;
}

expected this to work, unfortunately it didn't in my case for whatever reason

was on esbuild 0.19.7 downgraded to 0.13.15 which fixed it for now 🫣

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

Successfully merging a pull request may close this issue.

5 participants