how to import a CommonJS module as an ECMAScript module All In One
how to import a CommonJS module as an ECMAScript module All In One
CJS vs ESM
.mjs
import .cjs
module
cjs-module.cjs
const sum = (a, b) => a + b;
// export = sum;
// 'export =' can only be used in TypeScript files.ts(8003)
// ❌
// exports = sum;
// obj = {}
// sum = undefined
// ✅
// exports.sum = sum;
// console.log(`module inner sum`, sum);
// ✅
// module.exports = sum;
// ✅
module.exports = {sum};
esm-module.mjs
export const Add = (a, b) => a + b;
// ✅
// export default Add;
// ✅
export default {Add};
index.mjs
const log = console.log;
// ESM ✅
// import Add from './esm-module.mjs';
// import {Add as add} from './esm-module.mjs';
// log(`Add =`, Add);
// // Add = [Function: Add]
// log(`add =`, add);
// // add = [Function: Add]
import ESM from './esm-module.mjs';
import {Add as add} from './esm-module.mjs';
log(`ESM =`, ESM);
// ESM = { Add: [Function: Add] }
log(`add =`, add);
// add = [Function: Add]
const test1 = ESM.Add(3,4);
const test2 = add(3,4);
if(test1 === 7 && test2 === 7) {
log(`🚀✅ ESM.Add(3,4) && add(3,4) =`, test1, test2);
} else {
log(`🚀❌ error`, test);
}
// use CJS as ESM ✅
import obj from './cjs-module.cjs';
import {sum} from './cjs-module.cjs';
log(`obj =`, obj);
// obj = { sum: [Function: sum] }
log(`sum =`, sum);
// sum = [Function: sum]
const result = sum(1,2);
if(result === 3) {
log(`✅ sum(1,2) =`, result);
} else {
log(`❌ error`, result);
}
https://github.com/web-full-stack/how-to-use-cjs-module-as-esm-module-in-node.js
'@next/mdx'
https://github.com/vercel/next.js/blob/canary/packages/next-mdx/index.js
https://github.com/vercel/next.js/blob/canary/packages/next-mdx/index.d.ts
// HOF, Higher Order Function ✅
// CJS default export & anonymous functions
module.exports = (pluginOptions = {}) => (nextConfig = {}) => { return Object.assign({}, nextConfig, {...config})};
update docs for
ESM
usages
// rename the export anonymous function with whatever
import NextMdx from '@next/mdx'; // ✅
NextMdx()
OR
// alias
import * as NextMdx from '@next/mdx'; // ✅
NextMdx.default()
https://github.com/vercel/next.js/issues/43665#issuecomment-1340875080
??? PR for doccs
demos
fix: NextMdx is not a function bug ✅ NextMdx.default
https://github.com/vercel/next.js/issues/43665#
https://github.com/xgqfrms/ssr-next/commit/819d53c18cb1f876fd28fef4352ae96f31a0e4c6#r92243546
next.config.js
=> next.config.mjs
const path = require('path')
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
// i18n
i18n: {
locales: ['en', 'zh'],
// defaultLocale: 'en',
defaultLocale: 'zh',
// 默认值,不会显示在 url 路径中 (zh 不出现)
// http://localhost:3000/seo/test
// http://localhost:3000/en/seo/test
},
}
const withMDX = require('@next/mdx')({
extension: /\.mdx?$/,
options: {
remarkPlugins: [require("remark-prism")], // ✅
},
});
module.exports = withMDX({
// Append the default value with md extensions
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
...nextConfig,
sassOptions: {
includePaths: [
path.join(__dirname, 'styles')
],
},
})
import path from 'path'
import remarkPrism from "remark-prism"
import remarkGfm from "remark-gfm"
// import rehypeHighlight from 'rehype-highlight'
import * as NextMdx from '@next/mdx'; // ✅
// import {nextMDX as NextMdx} from '@next/mdx'; // ❌
// import pkg from '@next/mdx'; // ❌
// const {nextMDX: NextMdx} = pkg; // ❌
/*
CommonJS modules can always be imported via the default export, for example using:
import pkg from '@next/mdx';
const {nextMDX: NextMdx} = pkg;
*/
/** @type {import('next').NextConfig} */
// 默认当前 root 路径 ✅
const __dirname = path.resolve();
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
// i18n
i18n: {
locales: ['en', 'zh'],
// defaultLocale: 'en',
defaultLocale: 'zh',
// 默认值,不会显示在 url 路径中 (zh 不出现)
// http://localhost:3000/seo/test
// http://localhost:3000/en/seo/test
},
}
const withMDX = NextMdx.default({
// const withMDX = NextMdx({ // ❌ TypeError: NextMdx is not a function
// const withMDX = NextMdx.withMDX({ // ❌ TypeError: Cannot read properties of undefined (reading 'withMDX')
extension: /\.mdx?$/,
options: {
remarkPlugins: [remarkGfm, remarkPrism], // 二合一 ✅
// rehypePlugins: [rehypeHighlight],",
},
});
const CustomNextConfig = withMDX({
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
...nextConfig,
sassOptions: {
includePaths: [
path.join(__dirname, 'styles')
],
},
});
export default CustomNextConfig;
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
ESM
https://nodejs.org/api/esm.html#esm_enabling
https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples
https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm
https://juejin.im/post/6844904126195695624
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16964865.html
未经授权禁止转载,违者必究!