Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot resolve module 'module' in path/.../rewire/lib

Tags:

reactjs

flux

I am using rewire prior to require method in my React/Flux app, to load components. But after installation I got this error:

ERROR in ./~/rewire/lib/rewire.js
Module not found: Error: Cannot resolve module 'module' in /Users/path/node_modules/rewire/lib
 @ ./~/rewire/lib/rewire.js 1:13-30

ERROR in ./~/rewire/lib/rewire.js
Module not found: Error: Cannot resolve module 'fs' in /Users/path/node_modules/rewire/lib
 @ ./~/rewire/lib/rewire.js 2:9-22

ERROR in ./~/rewire/lib/moduleEnv.js
Module not found: Error: Cannot resolve module 'module' in /Users/path/node_modules/rewire/lib
 @ ./~/rewire/lib/moduleEnv.js 3:13-30

ERROR in ./~/rewire/lib/moduleEnv.js
Module not found: Error: Cannot resolve module 'fs' in /Users/path/node_modules/rewire/lib
 @ ./~/rewire/lib/moduleEnv.js 4:9-22

While googling I found similar issue, but no desirable solution. Does anyone know what is the issue or conflict about and how to resolve it?

like image 529
Max Avatar asked Jun 29 '26 08:06

Max


1 Answers

This is a webpack issue.

Since you're trying to use rewire, I'll assume you plan to run your tests through NodeJS and not a browser. Otherwise, check out rewire-webpack.

Rewire expects a NodeJS environment, but by default webpack targets a browser environment, and as such doesn't know about the standard NodeJS modules that rewire might depend on.

You need to set the target property of your webpack config to "node" in order to let webpack know it should consider standard NodeJS modules as externals.

like image 90
Alexandre Kirszenberg Avatar answered Jul 01 '26 11:07

Alexandre Kirszenberg