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

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

Closed
masx200 opened this issue Jun 3, 2021 · 15 comments
Labels
bug help welcome Could use help from community parser

Comments

@masx200
Copy link

masx200 commented Jun 3, 2021

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/*"
   }
}
@masx200 masx200 added bug help welcome Could use help from community parser labels Jun 3, 2021
@masx200
Copy link
Author

masx200 commented Jun 3, 2021


> require('@highlightjs/cdn-assets/highlight.min.js')
Uncaught:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './highlight.min.js' is not defined by "exports" in D:\Documents\github\markdown-reader\node_modules\@highlightjs\cdn-assets\package.json
    at new NodeError (node:internal/errors:363:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:321:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:546:3)
    at resolveExports (node:internal/modules/cjs/loader:478:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:518:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:27)
    at Function.Module._load (node:internal/modules/cjs/loader:774:27)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at REPL4:1:1 {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

@joshgoebel
Copy link
Member

Yeah this is a problem, certainly. Thanks for finding and filing all these issues, but curious - is there a reason you switched from the regular NPM package to cdn-assets now?

Right now I'm of the opinion that cdn-assets shouldn't have export restrictions at all (which would solve this)... but also most people bundling should probably be using the regular package (not con-assets) - so I'm truly curious why you're switching. :-)

@masx200
Copy link
Author

masx200 commented Jun 3, 2021

Yeah this is a problem, certainly. Thanks for finding and filing all these issues, but curious - is there a reason you switched from the regular NPM package to cdn-assets now?

Right now I'm of the opinion that cdn-assets shouldn't have export restrictions at all (which would solve this)... but also most people bundling should probably be using the regular package (not con-assets) - so I'm truly curious why you're switching. :-)

Because I want to use those built-in language modules directly.

@masx200
Copy link
Author

masx200 commented Jun 3, 2021

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.

@joshgoebel
Copy link
Member

Because I want to use those built-in language modules directly.

I don't understand. You can use them directly with the regular NPM package - you don't need to use cdn-assets.

joshgoebel added a commit to joshgoebel/highlight.js that referenced this issue Jun 4, 2021
joshgoebel added a commit that referenced this issue Jun 4, 2021
@joshgoebel
Copy link
Member

Resolved with release of 11.0.1.

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

@bitencode
Copy link

bitencode commented Jul 14, 2021

Not sure this is really fixed yet - it also happens on regular npm package. I assume it's a problem here because 10.x works but 11.x does not - I don't have this issue with any other npm package. I migrated to 11.0.0 when it came out and had to roll back to latest 10.x because of this problem. I upgraded to 11.1.0 this morning and still have this issue.

dependencies:

"dependencies": {
  ...
  "highlight.js": "11.1.0",
  "markdown-it": "12.1.0",
  "markdown-it-attrs": "4.0.0",
  ...
}

imports:

import MarkdownIt from 'markdown-it';
import MarkdownItAttrs from 'markdown-it-attrs';
import hljs from 'highlight.js/lib/core';
import 'highlight.js/styles/github.css';

hljs.registerLanguage('json', require('highlight.js/lib/languages/json'));
hljs.registerLanguage('markdown', require('highlight.js/lib/languages/markdown'));
hljs.registerLanguage('python', require('highlight.js/lib/languages/python'));
hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
hljs.registerLanguage('yaml', require('highlight.js/lib/languages/yaml'));

attempt to lint (build same problem, but not from eslint obviously):

Oops! Something went wrong! :(

ESLint: 7.29.0

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './styles/github.css' is not defined by "exports" in /Users/johnm/src/sdvi/swarm/node_modules/highlight.js/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:285:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:491:3)
    at resolveExports (internal/modules/cjs/loader.js:444:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:484:31)
    at findModulePath (/Users/johnm/src/sdvi/swarm/node_modules/eslint-import-resolver-alias/index.js:99:27)
    at Object.exports.resolve (/Users/johnm/src/sdvi/swarm/node_modules/eslint-import-resolver-alias/index.js:75:10)
    at v2 (/Users/johnm/src/sdvi/swarm/node_modules/eslint-module-utils/resolve.js:117:23)
    at withResolver (/Users/johnm/src/sdvi/swarm/node_modules/eslint-module-utils/resolve.js:122:14)
    at fullResolve (/Users/johnm/src/sdvi/swarm/node_modules/eslint-module-utils/resolve.js:139:22)
    at Function.relative (/Users/johnm/src/sdvi/swarm/node_modules/eslint-module-utils/resolve.js:84:10)
error Command failed with exit code 2.

However, this works, but is not right:

import MarkdownIt from 'markdown-it';
import MarkdownItAttrs from 'markdown-it-attrs';
import hljs from 'highlight.js/lib/core';
import '../../node_modules/highlight.js/styles/github.css';

hljs.registerLanguage('json', require('../../node_modules/highlight.js/lib/languages/json'));
hljs.registerLanguage('markdown', require('../../node_modules/highlight.js/lib/languages/markdown'));
hljs.registerLanguage('python', require('../../node_modules/highlight.js/lib/languages/python'));
hljs.registerLanguage('xml', require('../../node_modules/highlight.js/lib/languages/xml'));
hljs.registerLanguage('yaml', require('../../node_modules/highlight.js/lib/languages/yaml'));

Edit
Actually, the ../../node_modules/ bit makes eslint work, but both my current webpack and rollup builds fail with the path specified.

@dhruvkb
Copy link

dhruvkb commented Jul 25, 2021

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

@joshgoebel
Copy link
Member

joshgoebel commented Jul 25, 2021

There is never any reason to directly import lib/languages (there is nothing there, no index, etc) only files within that folder, which the wildcard subpath pattern should already cover. Having to specify the top-level directory in exports also seems broken and entirely redundant to me. I can find no indication in the documentation that subpath patterns should not be sufficient. Subpath folder mappings (as suggested above) are also deprecated...

https://nodejs.org/api/packages.html#packages_subpath_patterns

Perhaps file an issue against Webpack if that is what is kicking up the error?

@dhruvkb
Copy link

dhruvkb commented Jul 25, 2021

I did not import the lib/languages folder, I'm importing individual language files (by using a dynamic import expression). I'm quite confused by this error myself. The workarounds also seem to work without any reason or explanation.

@joshgoebel
Copy link
Member

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.

@azul
Copy link

azul commented Oct 27, 2021

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.

@joshgoebel
Copy link
Member

joshgoebel commented Oct 27, 2021

I wonder though if you changed your import from build-time to run-time since now the optimizer isn't smart enough to know the string at compile time?

@azul
Copy link

azul commented Oct 28, 2021

I wonder though if you changed your import from build-time to run-time since now the optimizer isn't smart enough to know the string at compile time?

That may have been the effect... but how would the compiler be smart enough to know lang at compile time? I think it should have imported at run-time anyway but tried at compile time never the less which led to the error because lang was empty at compile time.

(I don't really understand what's going on here - just guessing)

@joshgoebel
Copy link
Member

joshgoebel commented Oct 28, 2021

Sorry, no idea. Thanks for sharing though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community parser
Projects
None yet
Development

No branches or pull requests

5 participants