【问题标题】:Cannot find module "fs" - at webpackMissingModule找不到模块“fs” - 在 webpackMissingModule
【发布时间】:2019-06-11 12:30:27
【问题描述】:

我的服务器启动并正常运行,但是当我在浏览器中点击 URL 时,它会给出错误“找不到模块 'fs'”。

我尝试设置:

  1. target: 'node',但它开始另一个错误
  2. node: { fs: 'empty' },但是它给出了一个错误“找不到出口”

    "use strict";
    
    const webpack = require('webpack');
    const argv = require('minimist')(process.argv.slice(2));
    const DEBUG = !argv.release;
    
    const path = require('path');
    
    var plugins = [
      new webpack.optimize.CommonsChunkPlugin({
        names: ['common', 'vendors'],
        filename: '[name].js',
        minChunks: Infinity
      }),
      new webpack.optimize.OccurenceOrderPlugin(),
      new webpack.DefinePlugin({
        'process.env.NODE_ENV':  DEBUG ? '"development"' : '"production"',
        "process.argv.verbose": !!DEBUG
      }),
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        jquery: "jquery"
      })
    ].concat(DEBUG ? [] : [
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin({
        minimize: true,
        compress: {
          warnings: true
        }
      }),
      new webpack.optimize.AggressiveMergingPlugin()
    ]);
    
    module.exports = {
      entry: {
        app: path.join(__dirname, '..', 'app', 'app.js'),
        vendors: [
          'react',
          'react-dom',
          'react-bootstrap',
          'react-router',
          'alt',
          'lodash',
          'superagent',
          'react-router-role-authorization',
          'react-validation-decorator'
        ]
      },
    
      output: {
        publicPath: '/js/',
        path: './wwwroot/js/',
        filename: '[name].js',
        chunkFilename: "[id].[name].js"
      },
    
      context: path.join(__dirname, '..'),
    
      plugins: plugins,
    
      cache: DEBUG,
      debug: DEBUG,
      watch: DEBUG,
    
      stats: {
        colors: true
      },
    
      resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', '.json']
      },
    
      module: {
        loaders: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loaders: ['babel-loader']
          },
          {
            test: /\.(less|css)$/,
            loaders: ["style", "css", "less"]
          },
          {
            test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
            loader: 'url-loader?limit=10000'
          },
          {
            test: /\.(eot|ttf|wav|mp3|mp4)$/,
            loader: 'file-loader'
          },
          {
            test: /\.json$/,
            loader: 'json-loader'
          }
        ]
      },
      node: {
        net: 'mock',
        dns: 'mock'
      }
    };
    

它不应该给出这个错误并且可以正常工作。

【问题讨论】:

    标签: node.js webpack fs


    【解决方案1】:

    我在您发布的 webpack 设置中没有看到任何提及 fs 模块的内容。所以,我的猜测是您的输出应用程序 (app.js?) 正在尝试要求并使用 fs。 Webpack 正在构建一个客户端前端应用程序,该应用程序将加载到浏览器中; fs 在浏览器中不可用

    (仔细检查并确保您没有尝试在客户端应用程序中使用 fs 在用户计算机上读取和写入文件。这在基于浏览器的应用程序中是不可能的。有关具有前端和后端的 Web 应用程序概念的介绍,请查看文章 React App With Node Backend。)

    【讨论】:

    • 谢谢。我试图在前端需要一个后端模块
    猜你喜欢
    • 2018-02-10
    • 2018-05-07
    • 2018-07-10
    • 1970-01-01
    • 2017-02-12
    • 2017-09-06
    • 2021-08-22
    • 2021-09-06
    • 2021-07-15
    相关资源
    最近更新 更多