Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

javascript - Webpack Express Cannot Resolve Module 'fs', Request Dependency is Expression

When I include Express in my project I always get these errors when I try to build with 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'
      }
    ]
  }
}

I've tried:

  1. Installing 'fs' but it doesn't work
  2. Read somewhere to change the node fs property. It removes the error warnings but I don't think this is a good permanent solution.

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

    Time: 2496ms Asset Size Chunks Chunk Names bundle.js 761 kB 0 [emitted] server bundle.js.map 956 kB 0 [emitted] server + 119 hidden modules

    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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just posting an answer, since not everyone reads comments on SO. @Aurora0001 nailed it. Webpack's config needs to have this set:

"target": "node"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...