node.js - "Can' t resolve 'fs' in bindings”导入redis时?

标签 node.js webpack redis fs node-redis

我正在尝试使用 redis在 redis 表中查找键值的模块。然而,非常导入本身(import redis from 'redis')抛出以下错误:

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.

我尝试阅读模块的文档,但找不到任何关于此问题的引用资料。对于它的值(value),我的 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;
  },
})));

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

最佳答案

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

关于node.js - "Can' t resolve 'fs' in bindings”导入redis时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240214/

相关文章:

javascript - GraphQL : Reuse arguments from root_query in a type

node.js - 在 Sails.js/api/controllers 中创建子文件夹

css - Webpack:无法解码下载的字体

SpringMVC、MessageListener 和注入(inject)

javascript - Node.js 检查几个 redis 集中的值并同步返回

node.js - 无法登录到 GitHub 包注册表

node.js - 如何在 Jest 测试中发送 Node 命令行参数

javascript - 如何在 react 中导入ipcRenderer?

javascript - '--project' 在 webpack 命令行界面中意味着什么?

redis - 如何在 Redis 中查找字符串