Val-istar-Guo / rehype-prism

The unified plugin used to highlight code block in html with Prism
MIT License
12 stars 1 forks source link

Throws error in node 16+ using esm (missing file extension) #5

Closed daKmoR closed 2 years ago

daKmoR commented 2 years ago

Describe the bug

πŸ‘‰ package.json

"type": "module"

πŸ‘‰ index.js

import rehypePrism from 'rehype-prism'

Execute in the console

node index.js

Results in

node index.js
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/html/rehype-test/node_modules/rehype-prism/lib/src/index' imported from /html/rehype-test/node_modules/rehype-prism/lib/index.js
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:416:11)
    at moduleResolve (node:internal/modules/esm/resolve:932:10)
    at defaultResolve (node:internal/modules/esm/resolve:1044:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Expected behavior no error

Platform (please complete the following information):

Possible solution

adding file extensions to the generated js file makes it work

- export * from "./src/index";
+ export * from "./src/index.js";
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  /** @ts-ignore */
- import defaultExport from "./src/index";
+  import defaultExport from "./src/index.js";
export default defaultExport;
Val-istar-Guo commented 2 years ago

Thanks for your report. A new version has been released, upgrade to 1.0.1 and try again.