ReferenceError: process is not defined when importing .tsx files inside MDX file
See original GitHub issuemdx-bundler
version:^8.0.1
node
version:v17.3.0
npm
version: Usingyarn
version1.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`))
).toString();
const { code, frontmatter } = await bundleMDX<ArticleFrontmatter>({
source: mdxFile,
cwd: postsDirectory,
xdmOptions(options, frontmatter) {
options.remarkPlugins = [...(options.remarkPlugins ?? []), remarkMath];
options.rehypePlugins = [
...(options.rehypePlugins ?? []),
rehypeKatex,
rehypeHighlight,
];
return options;
},
});
validateFrontmatter(params!.slug, frontmatter);
if (!frontmatter.published) {
return {
notFound: true,
};
}
const now = new Date().toISOString();
const finalFrontmatter: ArticleMetadata = {
...frontmatter,
publishedOn: frontmatter.publishedOn ?? now,
updatedOn: frontmatter.updatedOn ?? null,
};
return {
props: {
code,
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
.
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:
- Created a year ago
- Reactions:1
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@voluntadpear thanks, it works for me. In my case, I add below in
bundleMDX()
:Just like https://github.com/FradSer/frad-me/commit/8c8cb421eddadd259769312edc46dd0e8c501fb3 .
I went with the approach suggested by @melosomelo but by using the actual values defined by Next:
Seems to work so far.