highlight.js: Module not found: Error: Package path ./highlight.min.js is not exported from package @highlightjs\cdn-assets

Describe the issue/behavior that seems buggy

Module not found: Error: Package path ./highlight.min.js is not exported from package D:\Documents\github\markdown-reader\node_modules@highlightjs\cdn-assets (see exports field in D:\Documents\github\markdown-reader\node_modules@highlightjs\cdn-assets\package.json)

Sample Code or Instructions to Reproduce

import hljs from "@highlightjs/cdn-assets/highlight.min.js";

export default hljs;

Expected behavior

Additional context

webpack 5.38.1 @highlightjs/cdn-assets 11.0.0

{
   "exports": {
      ".": {
         "require": "./lib/index.js",
         "import": "./es/index.js"
      },
      "./package.json": "./package.json",
      "./lib/common": {
         "require": "./lib/common.js",
         "import": "./es/common.js"
      },
      "./lib/core": {
         "require": "./lib/core.js",
         "import": "./es/core.js"
      },
      "./lib/languages/*": {
         "require": "./lib/languages/*.js",
         "import": "./es/languages/*.js"
      },
      "./scss/*": "./scss/*",
      "./styles/*": "./styles/*",
      "./types/*": "./types/*"
   }
}

About this issue

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

Commits related to this issue

Most upvoted comments

Using a separate variable to store the module name seems to have fixed this for me. So instead of

await import(
  /*
   webpackInclude: /(java|javascript|latex|markdown|python|ruby|typescript|xml|yaml)/,
   webpackChunkName: "lang-[request]"
   */
   `highlight.js/lib/languages/${lang}.js`
)

you would use:

const moduleName = `highlight.js/lib/languages/${lang}.js`
await import(
  /*
   webpackInclude: /(java|javascript|latex|markdown|python|ruby|typescript|xml|yaml)/,
   webpackChunkName: "lang-[request]"
   */
   moduleName
)

I have no idea why and if this actully works. Fixes my build and the example in the webpack issue though.

This seems to be broken for me as well. I’m getting the following error message:

Module not found: Error: Package path ./lib/languages is not exported from package <path_to_proj>/node_modules/highlight.js (see exports field in <path_to_proj>/node_modules/highlight.js/package.json)

This message appears when using the dynamic import syntax

await import(
  /*
   webpackInclude: /(java|javascript|latex|markdown|python|ruby|typescript|xml|yaml)/,
   webpackChunkName: "lang-[request]"
   */
   `highlight.js/lib/languages/${lang}.js`
)

The following are all solutions that fix the problem for me:

  1. Removing the exports field from package.json

  2. Adding one more export as follows:

     "./lib/languages/*": {
       "require": "./lib/languages/*.js",
       "import": "./es/languages/*.js"
     },
+    "./lib/languages": {
+      "require": "./lib/languages",
+      "import": "./es/languages"
+    },
  1. Referencing from as a relative path from the project directory also works:
 await import(
   /*
    webpackInclude: /(java|javascript|latex|markdown|python|ruby|typescript|xml|yaml)/,
    webpackChunkName: "lang-[request]"
    */
-   `highlight.js/lib/languages/${lang}.js`
+   `@/../node_modules/highlight.js/lib/languages/${lang}.js`
 )
  1. Downgrading to v10. This isn’t really a solution but it used to work before so, as this works as a last resort.

Downgrading to the latest v10 release fixes this problem for me. Removing the exports from v11 also fixes the problem. So something about the exports key in the package.json for v11 has to be the cause.

Framework: Vue 3 (Vue CLI 5) Language: TypeScript Bundler: Webpack 5 Node: 14 npm: 7

I realize that. That’s why I suggested file a bug against your packaging software. Of course you can link to this issue and we’ll see where things land.

Resolved with release of 11.0.1.

Exports are no longer restricted for cdn-assets, as this was never the intention.

import hljs from "../node_modules/@highlightjs/cdn-assets/highlight.min.js";

I found that as long as it is imported in this way, it works normally.