liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 61 forks source link

Module not found: Error: Can't resolve 'hiredis' #34

Open richburdon opened 7 years ago

richburdon commented 7 years ago

I use nodeExternals a lot to build server-side modules, but I get this error for any project that depends (indirectly) on redis (all other modules -- 1000s of them -- work fine).

In this case, my import of bull causes problems, through its dependency on ioredis (similar issues https://github.com/webpack/webpack-dev-server/issues/227 and https://github.com/NodeRedis/node_redis/issues/790)

Solution may relate to https://github.com/liady/webpack-node-externals/issues/29

ERROR in ../sub/scheduler/~/bull/~/redis-parser/lib/hiredis.js
Module not found: Error: Can't resolve 'hiredis' in '/.../scheduler/node_modules/bull/node_modules/redis-parser/lib'
 @ ../scheduler/~/bull/~/redis-parser/lib/hiredis.js 3:14-32
 @ ../scheduler/~/bull/~/redis-parser/lib/parser.js
 @ ../scheduler/~/bull/~/redis-parser/index.js
 @ ../scheduler/~/bull/~/ioredis/lib/redis/parser.js
 @ ../scheduler/~/bull/~/ioredis/lib/redis.js
 @ ../scheduler/~/bull/~/ioredis/index.js
 @ ../scheduler/~/bull/lib/queue.js
 @ ../scheduler/src/util/queue.js
 @ ../scheduler/index.js
 @ ./src/server/router/admin.js
 @ ./src/server/server.js
 @ ./src/server/main.js
 @ multi babel-polyfill ./src/server/main.js

The workaround is to add to "optionalDependencies" all modules that my other sub projects depend upon and set modulesFromFile but this is messy.

muratgozel commented 7 years ago

I have a simple webpack configuration for server side bundling and faced an issue exactly like yours. Adding nodeExternals() plugin solved the problem in my case. Here is related code in my webpack configuration:

{
  ...
  externals: [nodeExternals()],
  target: 'node',
  ...
}

It just skips all packages inside node_modules directory while bundling.

sprinklr-gurgaon commented 7 years ago

But why do you skip all packages inside node_modules while bundling? This looks like a workaround and not a proper fix?

muratgozel commented 7 years ago

Yes, it is not a proper fix but we don't need bundling in server side anyway. Not just packages inside node_modules folder, any package or module we import our code may not have to be bundled in server side since node supports es6 modules. Maybe redis lib maintainer clarify this problem better.

anilanar commented 5 years ago

@sprinklr-gurgaon Because most node-only libraries do lazy requireing using require (and not require.ensure, which is not part of node), which causes webpack to fail.