【问题标题】:"Can't resolve 'fs' in bindings" when importing redis?导入redis时“无法解析绑定中的'fs'”?
【发布时间】:2019-12-05 23:44:59
【问题描述】:

我正在尝试使用redis 模块在redis 表中查找键值。但是,导入本身 (import redis from 'redis') 会引发以下错误:

编译失败。

./node_modules/bindings/bindings.js 找不到模块:无法解析 '/home/ubuntu/proost/web/node_modules/bindings'中的'fs'

Build error occurred Error: > 由于 webpack 错误,构建失败 在构建(/home/ubuntu/proost/web/node_modules/next/dist/build/index.js:6:847) error 命令失败,退出代码为 1。

我尝试阅读该模块的文档,但找不到任何关于此问题的参考。值得一提的是,我的 next.config.js 文件(Webpack 的自定义 NextJS 扩展)如下所示:

/* 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;
  },
})));

有什么办法可以解决这个问题?

【问题讨论】:

    标签: node.js webpack redis fs node-redis


    【解决方案1】:

    在我看来,您正在尝试使用 Webpack 构建一个 JS 包,该包应该在浏览器的 JS 中使用。但是你不能将那个redis包用于浏览器的JS,它只能在Node.js中运行。

    【讨论】:

    • 但是我在服务器上调用了这个sn-p(if(!process.browser)。有什么办法让Webpack忽略这个错误?
    猜你喜欢
    • 2021-10-10
    • 2022-01-04
    • 1970-01-01
    • 2018-07-06
    • 2018-07-20
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2013-08-03
    相关资源
    最近更新 更多