【问题标题】:Uncaught ReferenceError: require is not defined at bundle.js(es-build)未捕获的 ReferenceError:未在 bundle.js(es-build) 中定义要求
【发布时间】:2023-02-02 16:24:16
【问题描述】:

我正在使用 esbuild API 构建来捆绑基本的 React 应用程序。但它不断出现以下错误Uncaught ReferenceError: require is not defined at bundle.js。下面是我的build.js代码。

const esbuild = require('esbuild');

async function build() {
  await esbuild.build({
    entryPoints: ['index.js'],
    outfile: './build/bundle.js',
    format: 'cjs',
    loader: { '.js': 'jsx' },
  });
}

build();

我尝试了什么:

  • 正在清除终端历史记录。
  • 删除bundle.js并重新生成
  • 正在重新启动 vs-code。

【问题讨论】:

  • 您是否尝试将格式更改为 iife
  • 不,让我试试看是否有效。谢谢
  • 不工作。我想我会继续努力。

标签: reactjs esbuild


【解决方案1】:

尝试将 require 的范围更改为您尝试使用它的范围。

【讨论】:

    【解决方案2】:

    我将const esbuild = require('esbuild');替换为import esbuild from 'esbuild';并将格式更改为iife。它奏效了。

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 2020-05-16
      • 2012-12-16
      • 2017-02-28
      • 2018-01-30
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多