Whenever I require redis inside a file other than the one where I start the server, I get the following error while webpack is bundling:
ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'net' in C:\NodeServer\AppInTheWild\node_modules\redis
@ ./~/redis/index.js 3:10-24
ERROR in ./~/redis/lib/parser/hiredis.js
Module not found: Error: Cannot resolve module 'hiredis' in C:\NodeServer\AppInTheWild\node_modules\redis\lib\parser
@ ./~/redis/lib/parser/hiredis.js 3:14-32
You can't bundle node-only libs (that do not provide browser shims). It is
impossible to run node-redis in the browser.
oh! What do you suggest me do then?
You simply cannot use node redis in your frontend build :).
Well sorry then, I am still learning. However, I know this is off topic, but I am able to import redis in my server.js file without any problems, but I cannot import it into any other files as it outputs the error I just mentioned.
I have looked on google about this but havent been able to find a tutorial or example that talks about this, that could explain what I am doing wrong and what the correct way is.
The few tutorials and examples I did find, they import redis into any of their files and it just works. That is why I thought it might be webpack causing this issue.
If I install net and hiredis, the errors go away but it outputs a warning that says it could not find the bindings file. I want to avoid having to install these dependencies as I have seen projects not using them while using redis.
edit: I forgot to include this issue: https://github.com/webpack/webpack-dev-server/issues/60
@Dindaleon using externals is the way to tell webpack not to include those deps into a build. But I still have a question, why you are using node-redis in your frontend code? If you are using it only for your server code, then why do you need webpack build there in the first place? Webpack is not supposed to bundle server-side code (well, it potentially could of course, but why?)
Some modules on npm cannot run in a web browser and some modules cannot run without a web browser. Generally if a module read/writes to a file system (as databases tend to do) or uses APIs native to OSs (such as require('net') or native addons) it will likely not run in a web browser. There is simply no compatible API in the web browser unless the module author has gone out of their way to provide one.
Think of it like asking, "Why can't I play Xbox games on my Nintendo?" Some games are designed to work on both systems, some only Xbox and some only Nintendo.
Same goes for node modules. Some only work with Node.js v0.12, some only io.js, some only on Chrome, some only on Firefox. Some work everywhere. Most of the time you just need to try it and find out.
yes, I had no clue. I was still learning about it. Thanks for your help and time.
I have the same issue as i am gettin gthis error:
Module not found: Error: Cannot resolve module 'hiredis' in /code/node_modules/redis/lib/parsers
For me the weird thing is that I dont get this error on my local machine but when i pushed this to github and ran on CircleCI, then im getting this error.
I'm pretty sure Redis is only on server side code, so im not sure why webpack is running into this issue. Anyone got solution to this?
Hey @bryanliu-gpsw, @Dindaleon. I know it has been a while but have a look here http://jlongster.com/Backend-Apps-with-Webpack--Part-I
I too use Webpack on the backend for several reasons, by using that configuration I was able to get the best of both worlds.
@claudiordgz I've used the following to build node modules with webpack
const nodeExternals = require('webpack-node-externals');
But with this and the articles you mention, I'm also getting this error; but only with redis. Did you specifically get this working?
@richburdon I haven't touch this in a while, bear with me, I did it here. I remember we had to do a map of the dependencies manually and pass them to the webpack config. Maybe it stopped working? I'll check it out tomorrow morning.
Hi @nkbt, why NOT use webpack to bundle/compress server side code (e.g., to build Docker containers). What do you think is intrinsically browser or "frontend" about webpack, babel, etc. minimization, language version transpiling, etc.) It generally works fine for me (and in fact is essential the the case of Dockerizing containers). But I'm eager to learn if you know of inherent limitations.
Thanks.
Most helpful comment
Hi @nkbt, why NOT use webpack to bundle/compress server side code (e.g., to build Docker containers). What do you think is intrinsically browser or "frontend" about webpack, babel, etc. minimization, language version transpiling, etc.) It generally works fine for me (and in fact is essential the the case of Dockerizing containers). But I'm eager to learn if you know of inherent limitations.
Thanks.