@@ -77,15 +77,25 @@ export function isBuildFailure(err: unknown): err is esbuild.BuildFailure {
77
77
* Rewrites esbuild BuildFailures for failing to resolve Node built-in modules
78
78
* to suggest enabling Node compat as opposed to `platform: "node"`.
79
79
*/
80
- export function rewriteNodeCompatBuildFailure ( err : esbuild . BuildFailure ) {
80
+ export function rewriteNodeCompatBuildFailure (
81
+ err : esbuild . BuildFailure ,
82
+ forPages = false
83
+ ) {
81
84
for ( const error of err . errors ) {
82
85
const match = nodeBuiltinResolveErrorText . exec ( error . text ) ;
83
86
if ( match !== null ) {
87
+ const issue = `The package "${ match [ 1 ] } " wasn't found on the file system but is built into node.` ;
88
+
89
+ const instructionForUser = `${
90
+ forPages
91
+ ? 'Add the "nodejs_compat" compatibility flag to your Pages project'
92
+ : 'Add "node_compat = true" to your wrangler.toml file'
93
+ } to enable Node.js compatibility.`;
94
+
84
95
error . notes = [
85
96
{
86
97
location : null ,
87
- text : `The package "${ match [ 1 ] } " wasn't found on the file system but is built into node.
88
- Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.` ,
98
+ text : `${ issue } \n${ instructionForUser } ` ,
89
99
} ,
90
100
] ;
91
101
}
@@ -148,6 +158,7 @@ export async function bundleWorker(
148
158
// TODO: Rip these out https://github.com/cloudflare/workers-sdk/issues/2153
149
159
disableModuleCollection ?: boolean ;
150
160
isOutfile ?: boolean ;
161
+ forPages ?: boolean ;
151
162
}
152
163
) : Promise < BundleResult > {
153
164
const {
@@ -179,6 +190,7 @@ export async function bundleWorker(
179
190
plugins,
180
191
disableModuleCollection,
181
192
isOutfile,
193
+ forPages,
182
194
} = options ;
183
195
184
196
// We create a temporary directory for any oneoff files we
@@ -412,7 +424,7 @@ export async function bundleWorker(
412
424
result = await esbuild . build ( buildOptions ) ;
413
425
} catch ( e ) {
414
426
if ( ! legacyNodeCompat && isBuildFailure ( e ) )
415
- rewriteNodeCompatBuildFailure ( e ) ;
427
+ rewriteNodeCompatBuildFailure ( e , forPages ) ;
416
428
throw e ;
417
429
}
418
430
0 commit comments