Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: process is not defined when importing .tsx files inside MDX file

See original GitHub issue
  • mdx-bundler version: ^8.0.1
  • node version: v17.3.0
  • npm version: Using yarn version 1.22.17

Relevant code or config

// root/pages/blog/[slug].tsx
export default function BlogArticle({ code, frontmatter }: ArticleProps) {
  const { asPath } = useRouter();
  const Component = useMemo(() => getMDXComponent(code), [code]); // error happens here
  return (...);
// ...
const getStaticProps: GetStaticProps<ArticleProps, PathDict> = async (ctx) => {
  const { params } = ctx;
  const postsDirectory = path.join(__dirname, "..", "..", "..", "..", "posts");
  const mdxFile = (
    await fs.promises.readFile(path.join(postsDirectory, `${params!.slug}.mdx`))
  const { code, frontmatter } = await bundleMDX<ArticleFrontmatter>({
    source: mdxFile,
    cwd: postsDirectory,
    xdmOptions(options, frontmatter) {
      options.remarkPlugins = [...(options.remarkPlugins ?? []), remarkMath];
      options.rehypePlugins = [
        ...(options.rehypePlugins ?? []),
      return options;
  validateFrontmatter(params!.slug, frontmatter);
  if (!frontmatter.published) {
    return {
      notFound: true,
  const now = new Date().toISOString();
  const finalFrontmatter: ArticleMetadata = {
    publishedOn: frontmatter.publishedOn ?? now,
    updatedOn: frontmatter.updatedOn ?? null,
  return {
    props: {
      frontmatter: finalFrontmatter,

What you did: I tried to import TSX components into an MDX file.

What happened: I got a client-side error that said ReferenceError: process is not defined. image

Problem description: I’m currently building my blog with MDX and NextJS. I’m on the final stages and I saw that I hadn’t configured the part with regards to the path resolution of component imports inside MDX files, so I tried following the docs and used the cwd option in the bundleMDX function, which is set to the directory where the MDX files live. All the imports within the MDX files are also relative to the posts directory. If I remove the imports, the error goes away. I don’t really know why I’m getting this error and I don’t know what I could do to fix it.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

FradSercommented, Jun 10, 2022

@voluntadpear thanks, it works for me. In my case, I add below in bundleMDX():

esbuildOptions: (options) => {
  options.define = {
    'process.env.__NEXT_TRAILING_SLASH': JSON.stringify(
    'process.env.__NEXT_IMAGE_OPTS': JSON.stringify(
    'process.env.__NEXT_REACT_ROOT': JSON.stringify(
    'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
  return options;

Just like .

voluntadpearcommented, May 17, 2022

I went with the approach suggested by @melosomelo but by using the actual values defined by Next:

options.define = {
      "process.env.__NEXT_TRAILING_SLASH": JSON.stringify(process.env.__NEXT_TRAILING_SLASH),
      "process.env.__NEXT_IMAGE_OPTS": JSON.stringify(process.env.__NEXT_IMAGE_OPTS),
      "process.env.__NEXT_REACT_ROOT": JSON.stringify(process.env.__NEXT_REACT_ROOT),
      "process.env.__NEXT_OPTIMIZE_FONTS": JSON.stringify(process.env.__NEXT_OPTIMIZE_FONTS)

Seems to work so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Uncaught ReferenceError: process is not defined
Today I added a very simple Modal component and somehow iframe started appearing. It appears when I am editing the file and hot...
Read more >
JavaScript - Parcel
Modules allow you to break up your code into different files, and share functionality between them by importing and exporting values.
Read more >
eslint parsing error: importdeclaration should appear when the ...
I'm using @typescript-eslint/parser in eslint for tsx files. I have this code in .tsx file: ts import React from 'react'; export class Wallboard...
Read more >
storybookjs/storybook (Raised $170.00) - Issuehunt
Attached MDX files choose the first indexed CSF file they reference as primary ... [Bug]: Angular function Input in component is not set...
Read more >
MDX - NextJS - codebook - Ryosuke
Write content as MDX. Either directly in /pages/ or import MDX into React pages. ... The plugin detects most MDX files in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found