kentcdodds / mdx-bundler

🦤 Give me MDX/TSX strings and I'll give you back a component you can render. Supports imports!
MIT License
1.77k stars 75 forks source link

Error when using esbuild@0.16.17 "Expected value for define "process.env.NODE_ENV" to be a string" #205

Open pkerschbaum opened 1 year ago

pkerschbaum commented 1 year ago

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: https://github.com/kentcdodds/mdx-bundler/blob/f1f48285e870bdc21e5eb0db8c34dcc0dd0d1f75/src/index.js#L179.
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.

danpacho commented 1 year ago

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

Toshinaki commented 1 year ago

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

riley-worthington commented 8 months ago

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 commented 7 months ago

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 🫣