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

Webpack+clone-deep: Cannot statically analyse 'require(…, …)' #30

Open
jschulte-xtl opened this issue Jan 19, 2024 · 0 comments
Open

Comments

@jschulte-xtl
Copy link

Hej,

I'm building a NestJS based API right now which uses the library nestjs-zod for schema based validation and that library in turn uses merge-deep which in turn uses clone-deep. That's in it self not a problem, but the problem arises when I'm trying to use NestJS within a Monorepo and therefore having to use Webpack in combination with the swc-loader loader.

The error message is something like the following:

ERROR in ../../node_modules/clone-deep/utils.js 13:0-28
Cannot statically analyse 'require(…, …)' in line 13
 @ ../../node_modules/clone-deep/index.js 7:12-30
 @ ../../node_modules/merge-deep/index.js 11:12-33
 @ ../../node_modules/nestjs-zod/dist/index.js 6:16-37

After a long night of googling-around, I found this Stackoverflow answer:

https://stackoverflow.com/questions/69898244/cannot-statically-analyse-require-in-line-16

and reading his answer I looked at the failing clone-deep source code within the utils.js where you find the following source-code:

'use strict';

/**
 * Lazily required module dependencies
 */

var utils = require('lazy-cache')(require);
var fn = require;

require = utils;
utils('is-plain-object', 'isObject');
require('shallow-clone', 'clone');
require('kind-of', 'typeOf');
require('for-own');
require = fn;

/**
 * Expose `utils`
 */

module.exports = utils;

And having read the Stackoverflow answer I changed the re-assignment of the require to something like this:

...
var utils = require('lazy-cache')(require);
utils('is-plain-object', 'isObject');
utils('shallow-clone', 'clone');
utils('kind-of', 'typeOf');
utils('for-own');
...

And that does the trick. Now Webpack can statically analyse the require function once again and the build goes through no problem.

This re-assignment of the require function seems to be happen more often, as I found another Issue where one reported the same problem but within another library. See:

webpack/webpack#15136

Should I open a Pull request for this or do you think it should be handled differently?

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

No branches or pull requests

1 participant