ant-design-icons: Missing support for type: "module" on Node?

I am trying to import icons from within a Node package with type: "module" specified in package.json:

import { BookOutlined } from '@ant-design/icons'

Unfortunately, this leads to the following error:

import { BookOutlined } from "@ant-design/icons";
         ^^^^^^^^^^^^
SyntaxError: Named export 'BookOutlined' not found. The requested module '@ant-design/icons' 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 '@ant-design/icons';
const { BookOutlined } = pkg;

Even import { BookOutlined } from '@ant-design/icons/es/index.js' does not work, because the package is interpreted as containing CommonJS modules.

The proposed solution is clumsy and also seems to break tree-shaking. My current workaround for each icon that I use is:

import BookOutlinedPackage from '@ant-design/icons/BookOutlined.js'
const BookOutlined = BookOutlinedPackage.default
export { BookOutlined }

This is obviously not a good solution. @ant-design/icons should probably be built using type: "module", specify an exports field in package.json and use imports with file extensions in the build.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 22 (4 by maintainers)

Most upvoted comments

@afc163 @zombieJ

此问题从 5.2.x 开始出现,代码的导出方式发生了变化,es 文件夹下的代码也全部失去了拓展名,使其无法像过去 5.1 通过 esm 模式引入,是一次严重的破坏性更新。

这使得原先不需要编译,通过 nodejs 18+ 直接运行的代码(一般用在单元测试中)在新版无法执行。

只能暂时锁定在 5.1版本规避,受此影响项目的 antd 也不得不停留在 5.8.0。

由于涉及到打包逻辑,普通人的PR估计你们也不会接受,请求官方跟进这个问题。

相关问题:https://github.com/ant-design/ant-design-icons/issues/307


This problem started in 5.2.x, the code exports changed, and the code under the es folder lost all extension names, making it impossible to introduce it through the ESM mode like in 5.1. It was a serious destructive update.

This makes code that was previously run directly with Nodejs 18+ without compilation (typically used in unit tests), became imexecutable in the new version.

It can only be temporarily locked in version 5.1 to avoid it, and the antd of the affected project has to stay at 5.8.0.

As it involves bundle logic, I don’t think you’ll accept PR for ordinary people, please official follow-up on this issue.

Related issues:https://github.com/ant-design/ant-design-icons/issues/307

Encountered same problem with remix + vite. Wondering how to workaround it.

To make it work with 5.2.6 and Remix 2+ I also found a possible workaround to be adding dependencies to remix.config.js:

serverDependenciesToBundle: [
    '@ant-design/icons',
    ^@ant-design\/icons-svg.*/,
    /^rc-util.*/,
  ],

This basically just compiles the packages into the resulting build/index.js and avoids having Node trying to resolve the packages.

Fixed in @ant-design/icons@5.4.0.

This seems to be a regression from 5.1.4 to 5.2.0

Rolling back to "@ant-design/icons": "5.1.4" worked for me, waiting for an official fix.