A project that uses esbuild fails as follows ...
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: []
}
... but if parcel is installed ...
npm install --save-dev parcel-bundler
... it succeeds.
The test is ...
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()
... but I've also tried running the following from the command-line with the same error (sans stack trace) ...
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
The offending require("events")
is coming from html-to-vdom
. My utility.js is small and contains the following import's ...
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'
I'm using esbuild v0.7.19. Help. What else should I provide?
Since the error message says set platform to "node" when building for node
, I assume that since you didn't do that this means you are bundling for the browser, not for node. The events
package is built in to node but it is not built in to the browser.
It looks like parcel-bundler
depends on node-libs-browser
which depends on crypto-browserify
which depends on events
. This explains why installing Parcel fixes this error. If your project needs to depend on the events
package (https://www.npmjs.com/package/events), you should install it yourself so it's part of your project:
npm install events