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

Cannot find module error with webpack@5 #25

Open
MikeDevice opened this issue Nov 11, 2020 · 11 comments · May be fixed by #37
Open

Cannot find module error with webpack@5 #25

MikeDevice opened this issue Nov 11, 2020 · 11 comments · May be fixed by #37

Comments

@MikeDevice
Copy link

MikeDevice commented Nov 11, 2020

Sometimes this plugin can break a build with an error when webpack@5 is used:

Cannot find module '<path-to-repo>/node_modules/<lib>/package.json'

It's easy to reproduce this issue. Just add to the project @babel/runtime dependency and try to make a build. The build will be broken with the follow error:

Error: Cannot find module '<path-to-repo>/node_modules/@babel/package.json'

Here is an example repo: webpack-file-dependencies-issue/tree/license-checker-webpack-plugin-example.

Initial issue: webpack/webpack#11971

@unindented
Copy link
Contributor

I don't think I understand the answer given in webpack/webpack#11971. What's the symlink that's causing this behavior?

@unindented
Copy link
Contributor

I could use your help creating a PR to fix this @MikeDevice.

@MikeDevice
Copy link
Author

I don't fully understand as well, but I figured out that resolve: { symlinks: false } solves the problem.

// webpack.config.js
module.exports = {
  resolve: { symlinks: false },
  plugins: [new LicenseCheckerWebpackPlugin()]
};

But of course it's not a solution at all. I think all we need is to check whether dependency has package.json or not before require it.

const getLicenseInformationForDependency = dependencyPath => {
const pkg = require(`${dependencyPath}/package.json`);

What do you think?

@unindented
Copy link
Contributor

Yup, that's probably what's causing the issue. Do you want to try and submit a PR to fix this?

@MikeDevice
Copy link
Author

Yes. I'll try.

@OpportunityLiu
Copy link

Fixed by changing filter to /(^.*[/\\]node_modules[/\\]((?:@[^@/\\]+[/\\])?(?:[^@/\\]+)))/, are we meeting the same issue?

@MikeDevice
Copy link
Author

@OpportunityLiu, Yes, we are, but I'm not sure, that this regex can cover all cases.

@MikeDevice
Copy link
Author

Maybe it's good to do something like that: Plugin Patterns

@Eli-Black-Work
Copy link

Eli-Black-Work commented Jan 21, 2021

I think the problem occurs whenever using a package in the form of @___/___, because we're also running into this problem, but when using the @apidevtools/json-schema-ref-parser package.

As soon as I add this line to a .jsx file:

import schemaRefParser from '@apidevtools/json-schema-ref-parser';

I get this error:

× 「wdm」: HookWebpackError: Qualified path resolution failed - none of the candidates can be found on the disk.
Source path: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.js
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.node
Require stack:
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js
- C:\my_app\webpack.config.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\webpack-cli.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\bootstrap.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\bin\cli.js
- C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\bin\webpack.js
    at makeWebpackError (C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\lib\HookWebpackError.js:49:9)
    at C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\lib\Compilation.js:2198:11
    at eval (eval at create (C:\my_app\.yarn\cache\tapable-npm-2.2.0-5c9edec1fb-f8ed725aed.zip\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:78:1)
-- inner error --
Error: Qualified path resolution failed - none of the candidates can be found on the disk.
Source path: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.js
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.node
Require stack:
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js
- C:\my_app\webpack.config.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\webpack-cli.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\bootstrap.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\bin\cli.js
- C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\bin\webpack.js
    at internalTools_makeError (C:\my_app\.pnp.js:15125:34)
    at resolveUnqualified (C:\my_app\.pnp.js:16158:13)
    at resolveRequest (C:\my_app\.pnp.js:16182:14)
    at Object.resolveRequest (C:\my_app\.pnp.js:16254:26)
    at Function.external_module_.Module._resolveFilename (C:\my_app\.pnp.js:15358:34)
    at Function.external_module_.Module._load (C:\my_app\.pnp.js:15223:48)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (C:\my_app\.yarn\cache\v8-compile-cache-npm-2.2.0-0b5b62067b-1efc994640.zip\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at getLicenseInformationForDependency (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:46:15)
    at C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:63:30
    at Array.reduce (<anonymous>)
    at getLicenseInformationForCompilation (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:59:27)
    at buildAsset (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js:16:28)
    at C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js:53:44
    at fn (C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\lib\Compilation.js:426:19)
    at _next0 (eval at create (C:\my_app\.yarn\cache\tapable-npm-2.2.0-5c9edec1fb-f8ed725aed.zip\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:70:17)
× 「wdm」: caused by plugins in Compilation.hooks.processAssets
Error: Qualified path resolution failed - none of the candidates can be found on the disk.
Source path: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.js
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.json
Rejected candidate: C:\my_app\.yarn\cache\@apidevtools-json-schema-ref-parser-npm-9.0.7-73540a30d3-2daf465e5d.zip\node_modules\@apidevtools\package.json.node
Require stack:
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js
- C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js
- C:\my_app\webpack.config.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\webpack-cli.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\lib\bootstrap.js
- C:\my_app\.yarn\$$virtual\webpack-cli-virtual-6906cc415f\0\cache\webpack-cli-npm-4.4.0-2000105866-0d1328369a.zip\node_modules\webpack-cli\bin\cli.js
- C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\bin\webpack.js
    at internalTools_makeError (C:\my_app\.pnp.js:15125:34)
    at resolveUnqualified (C:\my_app\.pnp.js:16158:13)
    at resolveRequest (C:\my_app\.pnp.js:16182:14)
    at Object.resolveRequest (C:\my_app\.pnp.js:16254:26)
    at Function.external_module_.Module._resolveFilename (C:\my_app\.pnp.js:15358:34)
    at Function.external_module_.Module._load (C:\my_app\.pnp.js:15223:48)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (C:\my_app\.yarn\cache\v8-compile-cache-npm-2.2.0-0b5b62067b-1efc994640.zip\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at getLicenseInformationForDependency (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:46:15)
    at C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:63:30
    at Array.reduce (<anonymous>)
    at getLicenseInformationForCompilation (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\licenseUtils.js:59:27)
    at buildAsset (C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js:16:28)
    at C:\my_app\.yarn\$$virtual\license-checker-webpack-plugin-virtual-eff79ca72f\0\cache\license-checker-webpack-plugin-npm-0.2.1-96e96069e5-f9dd7c6eed.zip\node_modules\license-checker-webpack-plugin\src\index.js:53:44
    at fn (C:\my_app\.yarn\$$virtual\webpack-virtual-213c98b382\0\cache\webpack-npm-5.16.0-fa17b04bcb-2709126257.zip\node_modules\webpack\lib\Compilation.js:426:19)
    at _next0 (eval at create (C:\my_app\.yarn\cache\tapable-npm-2.2.0-5c9edec1fb-f8ed725aed.zip\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:70:17)

It looks like license-checker-webpack-plugin is getting confused and looking for
\@apidevtools-json-schema-ref-parser-npm-random_string.zip\node_modules\@apidevtools\package.json
when it should be looking for
\@apidevtools-json-schema-ref-parser-npm-random_string.zip\node_modules\@apidevtools\json-schema-ref-parser\package.json

We're using Webpack 5 and Yarn Berry with the PnP (Plug and Play) feature enabled.

Hope this helps 🙂 Let me know you if I can provide any additional information.

bz2 added a commit to bz2/license-checker-webpack-plugin that referenced this issue Apr 4, 2021
Fixes microsoft#25

A valid node module name is either `\w+` or `@\w+/\w+` never `@\w+`.

This is enough of a hairy internal implementation detail that filter
should probably not be exposed even as an (undocumented) option. But
leaving alone for now as changing the pattern has been a useful
workaround.
@bz2 bz2 linked a pull request Apr 4, 2021 that will close this issue
@amuttsch
Copy link

amuttsch commented May 6, 2021

Another workaround is using the fixed filter from #37 when creating the plugin:

new LicenseCheckerWebpackPlugin({
    // Manually apply webpack5 fix from https://github.com/microsoft/license-checker-webpack-plugin/pull/37 &
    // https://github.com/microsoft/license-checker-webpack-plugin/issues/25
    filter: /(^.*[/\\]node_modules[/\\]((?:@[^/\\]+[/\\])?(?:[^@/\\][^/\\]*)))/,
    [...]
})

@sbytnar
Copy link

sbytnar commented Nov 20, 2021

#37 doesn't fix this problem for me.
I've tried both filter expressions before & after #37.

license-checker has been working fine for a while, but in the last day, it just stopped working. I haven't figured out which package related to webpack changed yet, but I suspect something in webpack.

When I make a debug plugin for webpack to look deeper, I see compilation.fileDependencies contains these non-existent files:

<path-to-repo>/node_modules/package.json/package.json
<path-to-repo>/node_modules/babel-loader.js/package.json
<path-to-repo>/node_modules/json-loader.js/package.json
<path-to-repo>/node_modules/yaml-loader.js/package.json
<path-to-repo>/node_modules/@svgr/package.json/package.json
<path-to-repo>/node_modules/@svgr/webpack.js/package.json

Those paths cause license-checker to throw exceptions.

But, these paths do exist: (note the @svgr/package.json doesn't exist.)

<path-to-repo>/node_modules/package-json/package.json
<path-to-repo>/node_modules/babel-loader/package.json
<path-to-repo>/node_modules/json-loader/package.json
<path-to-repo>/node_modules/yaml-loader/package.json

<path-to-repo>/node_modules/@svgr/webpack/package.json

If filter would accept a function OR a RegExp, a filter function could check for file existence or make other attempts at coercing the paths into ones that exist in the file system before the require() is called... but that may just hide the root cause problem of dealing with what looks like invalid paths showing up from webpack's fileDependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants