How to use codeHike with esm import in next.js
See original GitHub issueIssue Description
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:
- Created 10 months ago
- Comments:8 (4 by maintainers)
Top 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 >
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 Free
Top 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
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 #161Hmm, 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.