0

I'm trying to use the redis module to look up a key value in a redis table. However, the very import itself (import redis from 'redis') is throwing the following error:

Failed to compile.

./node_modules/bindings/bindings.js Module not found: Can't resolve 'fs' in '/home/ubuntu/proost/web/node_modules/bindings'

Build error occurred Error: > Build failed because of webpack errors at build (/home/ubuntu/proost/web/node_modules/next/dist/build/index.js:6:847) error Command failed with exit code 1.

I tried reading up the module's documentation but couldn't find any reference to this issue. For what it's worth, my next.config.js file (a custom NextJS extension of Webpack) looks like this:

/* eslint-disable no-unused-vars */

import path from 'path';
import glob from 'glob';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import webpack from 'webpack';
import dotenv from 'dotenv';
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import withSass from '@zeit/next-sass';
import withCSS from '@zeit/next-css';
import withPurgeCss from 'next-purgecss';

// dotenv.config();
const { parsed: localEnv } = dotenv.config();

module.exports = withCSS(withSass(withPurgeCss({
  distDir: '.build',
  purgeCssPaths: [
    'pages/**/*',
    'components/**/*',
  ],
  webpack: (config, { dev, isServer }) => {
    if (isServer) {
      return config;
    }
    config.plugins.push(
      new webpack.DefinePlugin({
        'process.env': {
          BASE_URL: JSON.stringify(process.env.BASE_URL),
          REDIS_HOST: JSON.stringify(process.env.REDIS_HOST),
          REDIS_PORT: JSON.stringify(process.env.REDIS_PORT),
        },
      }),
      new webpack.EnvironmentPlugin(localEnv),
      new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 1,
      }),
    );
    config.optimization.minimizer.push(
      new OptimizeCSSAssetsPlugin({}),
    );
    return config;
  },
})));

Any way to fix this problem?

1 Answer 1

0

Seems to me you're trying to build a JS bundle with Webpack which should be used in browser's JS. But you can't use that redis package for browser's JS, it's only to be run in Node.js.

1
  • But I'm calling this snippet on the server (if(!process.browser). Is there any way to make Webpack ignore this error?
    – TheLearner
    Jul 28, 2019 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.