当使用webpack时,Node找不到“fs”模块

5
我正在使用最新版本的node.js和webpack创建一个捆绑包。根据我的阅读,node.js应该包含用于管理文件的fs模块。然而,当我调用require("fs")时,我会收到“Cannot find module 'fs'”错误。在添加“target:'node'”后,我的控制台上出现了“require未定义”的问题。任何帮助都将是有用的,谢谢。

var webpack = require('webpack');

module.exports = {
    entry: "./client/main.js",
    output: {
        path: __dirname + '/public/build/',
        publicPath: "build/",
        filename: "bundle.js"
    },

    node: {fs: "empty"},

    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: "babel",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader!autoprefixer-loader",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.less$/,
                loader: "style-loader!css-loader!autoprefixer-loader!less",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.gif$/,
                loader: "url-loader?limit=10000&mimetype=image/gif"
            },
            {
                test: /\.jpg$/,
                loader: "url-loader?limit=10000&mimetype=image/jpg"
            },
            {
                test: /\.png$/,
                loader: "url-loader?limit=10000&mimetype=image/png"
            },
            {
                test: /\.svg/,
                loader: "url-loader?limit=26000&mimetype=image/svg+xml"
            },
            {
                test: /\.jsx$/,
                loader: "react-hot!babel",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            }
        ]
    },
    target: 'node',
}


https://dev59.com/glkS5IYBdhLWcg3w8am- - Adiii
这篇文章中的所有步骤都已经完成。 - Микола Стельмах
2个回答

4

3

不要将“fs”标记为空,尝试这样修饰它:

externals:{
    "fs": "commonjs fs"
}

这将告诉webpack将其作为模块加载,而不是将其视为环境对象/变量。
参考: 使用webpack时Node找不到模块“fs”

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接