question-mark
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.

How to use codeHike with esm import in next.js

See original GitHub issue

The next.js config is cjs file. But some remark plugins only provide esm packages, so i change my next.config.js to next.config.mjs and use it like:

import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';

const composedConfig = composePlugins([
  mdx({
    extension: /\.mdx?$/,
    options: {
      remarkPlugins: [
        remarkFrontmatter,
        // [remarkToc, { maxDepth: 2 }],
        remarkGfm,
      ],
      rehypePlugins: [rehypePrism, rehypeSlug],
      providerImportSource: '@mdx-js/react',
    },
  }),
  /** @type {import('next').NextConfig} */
  {
    reactStrictMode: true,
    // experimental: {
    //    reactMode: 'concurrent',
    // },
    experimental: {
      outputStandalone: true,
    },
    pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
  },
]);

I need use codeHike with remark plugins together, i tried:

import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import codeHike from '@code-hike/mdx';
const { remarkCodeHike } = codeHike;

const composedConfig = composePlugins([
  mdx({
    extension: /\.mdx?$/,
    options: {
      remarkPlugins: [
        remarkFrontmatter,
        // [remarkToc, { maxDepth: 2 }],
        [remarkCodeHike],
        remarkGfm,
      ],
      rehypePlugins: [rehypePrism, rehypeSlug],
      providerImportSource: '@mdx-js/react',
    },
  }),
  /** @type {import('next').NextConfig} */
  {
    reactStrictMode: true,
    // experimental: {
    //    reactMode: 'concurrent',
    // },
    experimental: {
      outputStandalone: true,
    },
    pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
  },
]);

Well, import is OK, but i got erros:

error - Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error
C:\Users\someone\Git\someproject\node_modules\@code-hike\mdx\dist\index.esm.js:1
import { FontStyle, setCDN, getHighlighter } from 'shiki';
^^^^^^

SyntaxError: Cannot use import statement outside a module

I need a little help

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pombercommented, Apr 21, 2022

Ok, @code-hike/mdx@0.4.0-next.6 should fix your issue. There’s an issue with React 18 though. I’m tracking it here #161

1reaction
pombercommented, Apr 15, 2022

Hmm, nextjs isn’t loading codehike as an es module. I’m going to rename codehike index.esm.js to .mjs and publish a new version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use 'import' with ESM packages instead #35217 - GitHub
Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals 2 | import path from 'path' 3 ...
Read more >
Using ESM in Next.js - Code Concisely
It's a way to include code from other JavaScript files, like npm packages, using the import keyword. // ES modules import { moduleFunction...
Read more >
Blog - Next.js 11.1
Starting with Next.js 11.1, you can now import npm packages using ES Modules (e.g. "type": "module" in their package.json ) with an ...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
The snippet above shows how you can import a module with dynamic imports and pass the ssr object as an argument to it....
Read more >
Import ES module in Next.js ERR_REQUIRE_ESM
I got it working by dynamically importing ky before using it but it's not elegant nor efficient. const handleFormSubmit = async (event) =>...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found