我正在尝试使用 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/