Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The requested module 'antd' is a CommonJS module, which may not support all module.exports as named exports. #38765

Closed
cevio opened this issue Nov 20, 2022 · 1 comment
Labels

Comments

@cevio
Copy link

cevio commented Nov 20, 2022

Reproduction link

https://github.com/cevio/antd5-esm-cjs

Steps to reproduce

$ git clone git@github.com:cevio/antd5-esm-cjs.git
$ cd antd5-esm-cjs
$ npm i --registry=https://registry.npmmirror.com/
$ node index.mjs

What is expected?

no errors.

What is actually happening?

import { Button } from 'antd';
         ^^^^^^
SyntaxError: Named export 'Button' not found. The requested module 'antd' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'antd';
const { Button } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
Environment Info
antd 5.0.0
React 18.0.0
System mac os
Browser chrome

mjs file module is not supported

sometimes, ssr bootstrap by .mjs, which like vite build --ssr command.

默认情况下,Vite 生成的 SSR 打包产物是 ESM 格式。实验性地支持配置 ssr.format ,但不推荐这样做。未来围绕 SSR 的开发工作将基于 ESM 格式,并且为了向下兼容,commonjs 仍然可用。如果你的 SSR 项目不能使用 ESM,你可以通过 Vite v2 外部启发式方法 设置 legacy.buildSsrCjsExternalHeuristics: true 生成 CJS 格式的产物。

https://cn.vitejs.dev/guide/ssr.html#ssr-format


using antd@4.x

import { Button } from 'antd';
console.log(Button);

no errors.

image

So this is a bug in antd@5.x

@zombieJ
Copy link
Member

zombieJ commented Nov 22, 2022

close by #38832

@zombieJ zombieJ closed this as completed Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants