简体   繁体   English

未捕获的 ReferenceError:未在 bundle.js(es-build) 中定义要求

[英]Uncaught ReferenceError: require is not defined at bundle.js(es-build)

I am using an esbuild API build to bundle a basic React app.我正在使用 esbuild API build 来捆绑一个基本的 React 应用程序。 But it keeps bringing up the following error Uncaught ReferenceError: require is not defined at bundle.js .Below is my build.js code.但它不断出现以下错误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();

What I tried:我尝试了什么:

  • Clearing terminal history.清除终端历史记录。
  • Deleting bundle.js and regenerating it again删除bundle.js并重新生成它
  • Restarting vs-code.重新启动 vs 代码。

Try to change the scope of require to the scope where you are trying to use it.尝试将 require 的 scope 更改为您尝试使用它的 scope 。

I replaced const esbuild = require('esbuild');我替换了const esbuild = require('esbuild'); with import esbuild from 'esbuild'; import esbuild from 'esbuild'; and changed the format to iife.并将格式更改为 iife。 And it worked.它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM