[ERROR] Could not resolve "node:buffer"

Hello, I'm having some issues with building my worker using:
compatibility_flags = [ "nodejs_compat" ]
compatibility_flags = [ "nodejs_compat" ]
I'm using a PNPM monorepo and the import i'm using is as follows:
import { Buffer } from 'node:buffer'
import { Buffer } from 'node:buffer'
I have everything running and working in dev mode, however when I go to build the worker I get the following error:
> node ./build.js

[ERROR] Could not resolve "node:buffer"

src/index.ts:1:23:
1 │ import { Buffer } from 'node:buffer'
~~~~~~~~~~~~~

The package "node:buffer" wasn't found on the file system but is built into node. Are you trying
to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
> node ./build.js

[ERROR] Could not resolve "node:buffer"

src/index.ts:1:23:
1 │ import { Buffer } from 'node:buffer'
~~~~~~~~~~~~~

The package "node:buffer" wasn't found on the file system but is built into node. Are you trying
to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
here is my build.js file with esbuild
import { build } from 'esbuild'

try {
await build({
entryPoints: ['./src/index.ts'],
bundle: true,
outdir: './dist/',
sourcemap: true,
minify: true,
conditions: ['worker', 'browser'],
outExtension: { '.js': '.mjs' },
format: 'esm',
target: 'esnext',
plugins: [
]
})
} catch (err) {
process.exitCode = 1
}
import { build } from 'esbuild'

try {
await build({
entryPoints: ['./src/index.ts'],
bundle: true,
outdir: './dist/',
sourcemap: true,
minify: true,
conditions: ['worker', 'browser'],
outExtension: { '.js': '.mjs' },
format: 'esm',
target: 'esnext',
plugins: [
]
})
} catch (err) {
process.exitCode = 1
}
3 Replies
Hello, I’m Allie!
Try
import { build } from 'esbuild'

try {
await build({
entryPoints: ['./src/index.ts'],
bundle: true,
outdir: './dist/',
sourcemap: true,
minify: true,
conditions: ['worker', 'browser'],
outExtension: { '.js': '.mjs' },
format: 'esm',
target: 'esnext',
plugins: [{
name: "nodejs_compat imports plugin",
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /node:.*/ }, () => {
return { external: true };
});
},
}],
})
} catch (err) {
process.exitCode = 1
}
import { build } from 'esbuild'

try {
await build({
entryPoints: ['./src/index.ts'],
bundle: true,
outdir: './dist/',
sourcemap: true,
minify: true,
conditions: ['worker', 'browser'],
outExtension: { '.js': '.mjs' },
format: 'esm',
target: 'esnext',
plugins: [{
name: "nodejs_compat imports plugin",
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /node:.*/ }, () => {
return { external: true };
});
},
}],
})
} catch (err) {
process.exitCode = 1
}
From Github
bateswebtech
bateswebtech10mo ago
@HardAtWork That did the trick, much appreciated!
maiku4823
maiku48237mo ago
Is there any way to get this working with sveltekit using the cloudflare-adapter?