0

使用 esbuild 的项目失败如下...

node_modules/htmlparser2/lib/Parser.js:133:36: error: Could not resolve "events" (set platform to "node" when building for node)
require("inherits")(Parser, require("events").EventEmitter);
                                    ~~~~~~~~
1 error
Error: Build failed with 1 error:
node_modules/htmlparser2/lib/Parser.js:133:36: error: Could not resolve "events" (set platform to "node" when building for node)
    at failureErrorWithLog (/Volumes/DUMBLEDORE1/Users/fficnar/Projects/Shared Projects/mablung-virtual-pug/node_modules/esbuild/lib/main.js:640:15)
    at /Volumes/DUMBLEDORE1/Users/fficnar/Projects/Shared Projects/mablung-virtual-pug/node_modules/esbuild/lib/main.js:504:31
    at handleIncomingPacket (/Volumes/DUMBLEDORE1/Users/fficnar/Projects/Shared Projects/mablung-virtual-pug/node_modules/esbuild/lib/main.js:486:9)
    at Socket.readFromStdout (/Volumes/DUMBLEDORE1/Users/fficnar/Projects/Shared Projects/mablung-virtual-pug/node_modules/esbuild/lib/main.js:429:7)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:303:12)
    at readableAddChunk (_stream_readable.js:279:9)
    at Socket.Readable.push (_stream_readable.js:218:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:188:23) {
  errors: [
    {
      location: [Object],
      text: 'Could not resolve "events" (set platform to "node" when building for node)'
    }
  ],
  warnings: []
}

...但是如果安装了包裹...

npm install --save-dev parcel-bundler

……它成功了。

测试是...

import '@virtualpatterns/mablung-source-map-support/install'
import _Bundler from 'esbuild'
import Path from 'path'

const { 'build': Bundle } = _Bundler
const FilePath = __filePath
const Require = __require

async function main() {

  try {

    await Bundle({
      'entryPoints': [ Require.resolve('../library/utility.js') ],
      'outfile': `process/esbuild${Path.extname(FilePath)}`,
      'minify': false,
      'bundle': true
    })
    
  } catch (error) {
    console.error(error)
  }

}

main()

...但我也尝试从命令行运行以下命令,但出现相同的错误(无堆栈跟踪)...

esbuild distributable-esmodule/library/utility.js --bundle --outdir=process/esbuild --sourcemap --platform=browser --log-level=info --strict
node_modules/htmlparser2/lib/Parser.js:133:36: error: Could not resolve "events" (set platform to "node" when building for node)
require("inherits")(Parser, require("events").EventEmitter);
                                    ~~~~~~~~
1 error

冒犯require("events")来自html-to-vdom. 我的utility.js 很小,包含以下导入的...

import _ConvertToVirtualNode from 'html-to-vdom'
import CreateVirtualNode from 'virtual-dom/h.js'
import VirtualNode from 'virtual-dom/vnode/vnode.js'
import VirtualText from 'virtual-dom/vnode/vtext.js'

我正在使用 esbuild v0.7.19。帮助。我还应该提供什么?

4

1 回答 1

1

由于错误消息说set platform to "node" when building for node,我认为由于您没有这样做,这意味着您正在捆绑浏览器,而不是 node.js 。该events包内置于节点中,但未内置于浏览器中。

看起来parcel-bundler取决于取决于node-libs-browser哪个取决于crypto-browserify哪个取决于events。这解释了为什么安装 Parcel 会修复此错误。如果您的项目需要依赖events包 ( https://www.npmjs.com/package/events ),您应该自己安装它,使其成为您项目的一部分:

npm install events
于 2020-10-24T07:17:36.183 回答