【问题标题】:ReferenceError: window is not defined with ReactJSReferenceError:没有用 ReactJS 定义窗口
【发布时间】:2018-11-06 13:23:10
【问题描述】:

晚上好,我在 Ubuntu 上使用 react 16.3.2,我想将我的新 react 应用程序与 this software 集成。

我的 webpack.fly.config.js 文件:

const path = require("path");
const webpack = require("webpack");
const bundlePath = path.resolve(__dirname, "dist/");

module.exports = {
  entry: "./src/index.js",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
query: { presets: ['react']}
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      }
    ]
  },
  resolve: { extensions: ['*', '.js', '.jsx'] },
  output: {
    publicPath: bundlePath,
    filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname,'public'),
    port: 3000,
    publicPath: "http://localhost:3000/dist"
  },
  plugins: [ new webpack.HotModuleReplacementPlugin() ]
};

我认为这不是问题,因为当我运行 fly server 时服务器开始运行,但是当我去刷新我的 localhost 网页时,我收到以下错误:

ReferenceError: 未定义窗口 在 module.exports (bundle.js:5:41) 在 bundle.js:728:10 在 () 在 /usr/local/lib/node_modules/@fly/fly/lib/default_context_store.js:47:30

我的 index.js 文件:

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('root')
);

还有我的 index.html:

<!-- sourced from https://raw.githubusercontent.com/reactjs/reactjs.org/master/static/html/single-file-example.html -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>React Starter</title>
  </head>
  <body>
    <div id="root"></div>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <script src="../dist/bundle.js"></script>
  </body>
</html>

React 和 Webpack 对我来说仍然很新,所以我什至不确定这个错误是什么或它意味着什么。

有人可以在这里阐明一下并指出正确的方向吗?

【问题讨论】:

  • 什么是苍蝇?????
  • 看起来像 express,一个 nodejs 服务器...
  • Fly 是一家 JavaScript CDN 初创公司 - 请参阅此处 fly.io。我正在努力将他们的技术与 React 集成,因为它们齐头并进。
  • 我猜你或它(软件)不小心包含了不应该包含在服务器端的文件
  • 谢谢帕特里克,你能详细说明一下吗?我会在哪里告诉它?在配置文件中?

标签: javascript reactjs webpack fly


【解决方案1】:

在您的代码中搜索“窗口”并确保它在客户端使用 !SERVER 或类似命令时执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2020-10-08
    • 2018-05-27
    • 2020-09-18
    • 1970-01-01
    • 2020-11-12
    • 2018-10-29
    相关资源
    最近更新 更多