Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/c3-node-version-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-cloudflare": patch
---

Show a clear error message when running on an unsupported Node.js version

Previously, running `create-cloudflare` on an older Node.js version (e.g. v18) would fail with a confusing syntax error. Now, a dedicated version check runs before loading the CLI and displays a helpful message explaining the minimum required Node.js version and suggesting version managers like Volta or nvm.
9 changes: 5 additions & 4 deletions packages/create-cloudflare/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

## OVERVIEW

Project scaffolding CLI for Cloudflare Workers. Single entry: `src/cli.ts` serves as CLI, library export, and bin target.
Project scaffolding CLI for Cloudflare Workers. Main source entry: `src/cli.ts`. Bin entry: `bin/c3.js` (Node.js version gate shim).

## STRUCTURE

- `src/cli.ts` — Main entry. Exports `main(argv)` for programmatic use, has shebang for direct execution
- `bin/c3.js` — Bin shim that checks Node.js version before requiring `dist/cli.js`
- `src/cli.ts` — Main entry. Exports `main(argv)` for programmatic use
- `templates/` — Scaffolding templates (excluded from linting and most formatting)
- `scripts/build.ts` — esbuild-based build → `dist/cli.js`

## BUILD

- esbuild bundles `src/cli.ts` as CJS → `dist/cli.js`
- `package.json` `main`, `exports["."]`, and `bin` all point at `dist/cli.js`
- No separate bin shim — the built output IS the bin
- `package.json` `main` and `exports["."]` point at `dist/cli.js`; `bin` points at `bin/c3.js`
- `bin/c3.js` is a plain CommonJS shim that gates on Node.js version (read from `package.json` `engines.node`) before requiring `dist/cli.js`

## CONVENTIONS

Expand Down
54 changes: 54 additions & 0 deletions packages/create-cloudflare/bin/c3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

const MIN_NODE_VERSION = require("../package.json").engines.node.replace(
">=",
""
);

// semiver implementation via https://github.com/lukeed/semiver/blob/ae7eebe6053c96be63032b14fb0b68e2553fcac4/src/index.js

/**
MIT License

Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

var fn = new Intl.Collator(0, { numeric: 1 }).compare;

function semiver(a, b, bool) {
a = a.split(".");
b = b.split(".");

return (
fn(a[0], b[0]) ||
fn(a[1], b[1]) ||
((b[2] = b.slice(2).join(".")),
(bool = /[.-]/.test((a[2] = a.slice(2).join(".")))),
bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1)
);
}

// end semiver implementation

function main() {
if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
console.error(
`create-cloudflare requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of Node.js.

Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
);
process.exitCode = 1;
return;
}

require("../dist/cli.js");
}

main();
5 changes: 3 additions & 2 deletions packages/create-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"url": "https://github.com/cloudflare/workers-sdk.git",
"directory": "packages/create-cloudflare"
},
"bin": "./dist/cli.js",
"bin": "./bin/c3.js",
"files": [
"bin",
"dist",
"templates",
"templates-experimental"
Expand Down Expand Up @@ -89,7 +90,7 @@
"yargs": "^17.7.2"
},
"engines": {
"node": ">=18.14.1"
"node": ">=20.0.0"
},
"volta": {
"extends": "../../package.json"
Expand Down
65 changes: 65 additions & 0 deletions packages/create-cloudflare/src/__tests__/check-node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, test } from "vitest";

const binPath = resolve(__dirname, "../../bin/c3.js");
const pkgPath = resolve(__dirname, "../../package.json");
const minNodeVersion = JSON.parse(
readFileSync(pkgPath, "utf-8")
).engines.node.replace(">=", "");

describe("check-node version gate", () => {
test("outputs a useful error message when Node.js version is too old", ({
expect,
}) => {
// We can't actually run on an old Node, but we can verify the bin file
// contains the expected version gate by executing it with a mock that
// overrides process.versions.node. We do this via --eval.
const script = `
// Override the node version to simulate an old version
Object.defineProperty(process, "versions", {
value: { ...process.versions, node: "18.0.0" },
});
require(${JSON.stringify(binPath)});
`;

try {
execFileSync(process.execPath, ["--eval", script], {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
// If it doesn't throw, that's unexpected
expect.unreachable("Expected the process to exit with code 1");
} catch (e: unknown) {
const error = e as { status: number; stderr: string };
expect(error.status).toBe(1);
expect(error.stderr).toContain(
`create-cloudflare requires at least Node.js v${minNodeVersion}`
);
expect(error.stderr).toContain("You are using v18.0.0");
expect(error.stderr).toContain("https://volta.sh/");
}
});

test("does not error when Node.js version meets the minimum", ({
expect,
}) => {
// Running the bin file with the current Node.js (which is >= 20)
// should not produce the version error. It will fail because
// dist/cli.js may not exist, but that's a different error.
try {
execFileSync(process.execPath, [binPath], {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
timeout: 5000,
});
} catch (e: unknown) {
const error = e as { stderr: string };
// Should NOT contain the version error
expect(error.stderr).not.toContain(
"create-cloudflare requires at least Node.js"
);
}
});
});
Loading