Skip to content

Commit

Permalink
Add a targeted error message suggesting the 'use node' directive (#32…
Browse files Browse the repository at this point in the history
…710)

<img width="707" alt="Screenshot 2025-01-02 at 17 17 03" src="https://github.com/user-attachments/assets/f79a363c-ce8d-4c14-a6cf-b94b76964b4d" />

This is a semi-common error message (often from a library that depends on node, or some code copy pasted in that required Node) and it's not obvious that the solution is `"use node"`.

This tries to at least detect the error message and suggest `"use node"`. There might be more advanced ways of detecting which source file / which import might be causing the issue, but this seems like a good start.

GitOrigin-RevId: 5e8c1b0c8614ace83c0d80787ed5f68a86fd5ce4
  • Loading branch information
sshader authored and Convex, Inc. committed Jan 3, 2025
1 parent 676fddd commit 3afaf19
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion npm-packages/convex/src/bundler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,34 @@ async function doEsbuild(
} catch (e: unknown) {
// esbuild sometimes throws a build error instead of returning a result
// containing an array of errors. Syntax errors are one of these cases.
let recommendUseNode = false;
if (isEsbuildBuildError(e)) {
for (const error of e.errors) {
if (error.location) {
const absPath = path.resolve(error.location.file);
const st = ctx.fs.stat(absPath);
ctx.fs.registerPath(absPath, st);
}
if (
platform !== "node" &&
!recommendUseNode &&
error.notes.some((note) =>
note.text.includes("Are you trying to bundle for node?"),
)
) {
recommendUseNode = true;
}
}
}
return await ctx.crash({
exitCode: 1,
errorType: "invalid filesystem data",
// We don't print any error because esbuild already printed
// all the relevant information.
printedMessage: null,
printedMessage: recommendUseNode
? `It looks like you are using Node APIs from a file without the "use node" directive.\n` +
`See https://docs.convex.dev/functions/runtimes#nodejs-runtime`
: null,
});
}
}
Expand Down

0 comments on commit 3afaf19

Please sign in to comment.