highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

es模块导出方式不正确,导致项目构建编译报错[!] Error: 'default' is not exported by node_modules/highlight.js/lib/index.js, imported by node_modules/highlight.js/es/index.js

Open hqqxxf opened this issue 1 year ago • 4 comments

es模块引用了lib/index.js,而lib/index.js是commonjs的导出方式 image

image

hqqxxf avatar Sep 15 '23 03:09 hqqxxf

Yet default is exported you can see it right there... how are you trying to use this in your project?

joshgoebel avatar Sep 15 '23 04:09 joshgoebel

Oh, I had this issue too (but had written it off to an oddity of our setup).

For some more colour; our project is a mono repo which has a "platform" and a "ui lib" (which is used by the platform with a package entry of "ui": "file:../ui"). The UI lib is the one which has the highlight.js dependency and a Highlight component.

Also note that we use Vite.

My working theory had been that the highlight.js package.json uses exports which alias require and import definitions, and maybe those are getting lost?

adamburmister avatar Sep 17 '23 22:09 adamburmister

Perhaps it's a vite issue?

joshgoebel avatar Sep 18 '23 01:09 joshgoebel

Got here b/c I ran into this trying to use highlight.js with @web/dev-server(for dev - which gave me a The requested module './../core.js' does not provide an export named default error) and rollup(for build - which gives the same error as in the title - just core.js instead of index).

Neither tool handles commonjs module conversion by default anymore. Have to setup @rollup/plugin-commonjs and - for dev-server at least - explicitly tell it about the highlight.js package.

Can't speak to vite, I'm not using it. The docs say that it handles commonjs module conversion by default, but I wouldn't be surprised if it didn't include node_modules in that. Might need to tell it to convert the package.

AbandonedFridge avatar Nov 08 '23 07:11 AbandonedFridge

Have to setup @rollup/plugin-commonjs

That sounds like a reasonable solution in the interim. This should also be fixed when we hit version 12 and only ship ES builds.

Closing this as #wontfix.

joshgoebel avatar Mar 19 '24 04:03 joshgoebel