mdx-bundler
mdx-bundler copied to clipboard
Error when using [email protected] "Expected value for define "process.env.NODE_ENV" to be a string"
-
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: 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.
In my case, this problem is fixed via installing esbuild
version 0.13.15
.
I'm facing the same issue here. Why the fix is not merged?
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;
}
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 tobundleMDX
: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 🫣