Webpack Express无法解析模块“ fs”,请求依赖关系为表达式

当我在项目中包含Express时,尝试使用webpack进行编译时总是会遇到这些错误。

webpack.config.dev.js

var path = require("path")

module.exports = {
  entry: {
    "server": "./server/server.ts"
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
    publicPath: "/public/"
  },
  module: {
    loaders: [
      {
        test: /\.ts(x?)$/,
        exclude: /node_modules/,
        loader: "ts-loader"
      }, {
        test: /\.js(x?)$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      }, {
        test: /\.json$/,
        loader: "json-loader"
      }, {
        test: /\.scss$/,
        exclude: /node_modules/,
        loaders: ["style-loader", "css-loader", "postcss-loader", "sass-loader"]
      }, {
        test: /\.css$/,
        loader: ["style-loader", "css-loader", "postcss-loader"]
      }, {
        test: /\.(jpe?g|gif|png|svg)$/i,
        loader: 'url-loader?limit=10000'
      }
    ]
  }
}

我试过了:

  1. 安装“ fs”,但不起作用
  2. 阅读某处以更改节点fs属性。它消除了错误警告,但我认为这不是一个很好的永久解决方案。

    module.exports = {
      node: {
        fs: "empty"
      }
    }
    

    时间:2496ms资产大小块块名称bundle.js 761 kB 0 [已发射]服务器bundle.js.map 956 kB 0 [已发射]服务器+ 119个隐藏模块

    WARNING in ./~/express/lib/view.js Critical dependencies: 78:29-56 the request of a dependency is an expression @ ./~/express/lib/view.js 78:29-56 ERROR in ./~/express/lib/view.js

    Module not found: Error: Cannot resolve module 'fs' in /Users/clementoh/Desktop/boilerplate2/node_modules/express/lib @ ./~/express/lib/view.js 18:9-22 ERROR in ./~/send/index.js

    Module not found: Error: Cannot resolve module 'fs' in /Users/clementoh/Desktop/boilerplate2/node_modules/send @ ./~/send/index.js 24:9-22 ERROR in ./~/etag/index.js

    Module not found: Error: Cannot resolve module 'fs' in /Users/clementoh/Desktop/boilerplate2/node_modules/etag @ ./~/etag/index.js 22:12-25 ERROR in ./~/destroy/index.js

    Module not found: Error: Cannot resolve module 'fs' in /Users/clementoh/Desktop/boilerplate2/node_modules/destroy @ ./~/destroy/index.js 14:17-30 ERROR in ./~/mime/mime.js

    Module not found: Error: Cannot resolve module 'fs' in /Users/clementoh/Desktop/boilerplate2/node_modules/mime @ ./~/mime/mime.js 2:9-22

卡卡西2020/04/07 11:43:20

通过将"target": "node",作品添加到中来添加作品webpack.config.js

小小2020/04/07 11:43:20

只是发布答案,因为不是每个人都阅读有关SO的评论。@ Aurora0001钉了它。Webpack的配置需要进行以下设置:

"target": "node"