使用Webpack构建ReactJS项目会导致graceful-js报告错误"无法解析' fs'""

时间:2017-03-24 21:16:47

标签: node.js reactjs webpack

昨晚我正在建设我的项目。它是一个使用webpack构建React可分发前端代码的Node项目。当我完成后,我关闭了我的电脑。我今天早上打开了我的电脑,现在没有代码或配置更改我的知识,我得到错误,缺少某种依赖关系。

ERROR in ./~/fsevents/~/graceful-fs/fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/stevenkitzes/Documents/Career Dev/C2 to A1/react-lnl/demo/react-lnl/node_modules/fsevents/node_modules/graceful-fs'
 @ ./~/fsevents/~/graceful-fs/fs.js 3:9-22
 @ ./~/fsevents/~/graceful-fs/graceful-fs.js
 @ ./~/fsevents/~/fstream/lib/reader.js
 @ ./~/fsevents/~/fstream/fstream.js
 @ ./~/fsevents/~/tar-pack/index.js
 @ ./~/fsevents/~/node-pre-gyp/lib/package.js
 @ ./~/fsevents/~/node-pre-gyp/lib ^\.\/.*$
 @ ./~/fsevents/~/node-pre-gyp/lib/node-pre-gyp.js
 @ ./~/fsevents/fsevents.js
 @ ./~/chokidar/lib/fsevents-handler.js
 @ ./~/chokidar/index.js
 @ ./~/watchpack/lib/DirectoryWatcher.js
 @ ./~/watchpack/lib/watcherManager.js
 @ ./~/watchpack/lib/watchpack.js
 @ (webpack)/lib/node/NodeWatchFileSystem.js
 @ (webpack)/lib ^.*$
 @ (webpack)/lib/webpack.js
 @ ./jsx-map/jsx-build.js

这是我的webpack配置文件:

var webpack = require('webpack');
var path = require('path');

var APP_DIR = path.resolve(__dirname, '.');

var plugins = [];

var config = {
  entry: APP_DIR + '/jsx.js',
  output: {
    path: APP_DIR,
    filename: 'jsx-out.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?/,
        exclude: /node_modules/,
        include: APP_DIR,
        loader: 'babel-loader',
        query: {
          presets: ['react']
        }
      }
    ]
  },
  plugins: plugins
};

module.exports = config;

1 个答案:

答案 0 :(得分:0)

听起来您可能正在尝试将服务器端(节点)和客户端(反应内容)捆绑在webpack中。如果需要,您需要单独捆绑它们以创建2个不同的捆绑文件。

  • 一个用于执行节点的节点,例如<script src='./bundle-client.js'></script>(假设您的服务器端捆绑包生成为'bundle-server.js')
  • 另一个包含在您的前端html代码中的内容,例如ifelse(假设您的客户端捆绑包生成为'bundle-client.js')。

具体来说,您需要在webpack配置文件中设置选项,使其具有两个“输出”和两个“目标”,一个用于服务器端,另一个用于客户端。

有关使用webpack进行客户端和服务器端捆绑的详细信息,请参阅https://medium.com/code-oil/webpack-javascript-bundling-for-both-front-end-and-back-end-b95f1b429810