Skip to content

The open alternative to Salesforce, designed for AI.

fix(cli): explain the Node requirement instead of crashing on unsupported Node versions (#27520)

Weiko748e88a2d360
diff --git a/packages/twenty-cli/src/__tests__/start-cli.spec.ts b/packages/twenty-cli/src/__tests__/start-cli.spec.ts
new file mode 100644
index 00000000000..7be874e1ff8
--- /dev/null
+++ b/packages/twenty-cli/src/__tests__/start-cli.spec.ts
@@ -0,0 +1,53 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { engines } from '../../package.json';
+
+import { startCli } from '@/start-cli';
+
+describe('startCli', () => {
+ afterEach(() => {
+ process.exitCode = undefined;
+ vi.restoreAllMocks();
+ });
+
+ it('runs the loaded CLI with the command line arguments', async () => {
+ const runCli = vi.fn(async () => undefined);
+
+ await startCli({
+ args: ['version'],
+ nodeVersion: process.versions.node,
+ loadCli: async () => ({ runCli }),
+ });
+
+ expect(runCli).toHaveBeenCalledWith(['version']);
+ });
+
+ it('explains the Node requirement when the CLI cannot load on an unsupported Node', async () => {
+ const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
+
+ await startCli({
+ args: ['version'],
+ nodeVersion: '18.17.1',
+ loadCli: async () => {
+ throw new Error('crypto.getRandomValues() not supported.');
+ },
+ });
+
+ expect(stderr).toHaveBeenCalledWith(
+ `twenty requires Node.js ${engines.node}; this is Node.js 18.17.1. Switch to a supported Node.js version, then try again.\n`,
+ );
+ expect(process.exitCode).toBe(1);
+ });
+
+ it('keeps load failures on a supported Node visible', async () => {
+ await expect(
+ startCli({
+ args: ['version'],
+ nodeVersion: process.versions.node,
+ loadCli: async () => {
+ throw new Error('broken bundle');
+ },
+ }),
+ ).rejects.toThrow('broken bundle');
+ });
+});
diff --git a/packages/twenty-cli/src/cli.ts b/packages/twenty-cli/src/cli.ts
index cf4f22d7074..baffdd0a26e 100644
--- a/packages/twenty-cli/src/cli.ts
+++ b/packages/twenty-cli/src/cli.ts
@@ -1,5 +1,5 @@
#!/usr/bin/env node
-import { runCli } from '@/run-cli';
+import { startCli } from '@/start-cli';
const exitWhenStdoutCloses = (error: NodeJS.ErrnoException) => {
if (error.code !== 'EPIPE') {
@@ -11,4 +11,8 @@ const exitWhenStdoutCloses = (error: NodeJS.ErrnoException) => {
process.stdout.on('error', exitWhenStdoutCloses);
-void runCli(process.argv.slice(2));
+void startCli({
+ args: process.argv.slice(2),
+ nodeVersion: process.versions.node,
+ loadCli: () => import('@/run-cli'),
+});
diff --git a/packages/twenty-cli/src/start-cli.ts b/packages/twenty-cli/src/start-cli.ts
new file mode 100644
index 00000000000..d1e9ef152d5
--- /dev/null
+++ b/packages/twenty-cli/src/start-cli.ts
@@ -0,0 +1,37 @@
+import { engines } from '../package.json';
+
+import { checkNodeRequirement } from '@/app/project/check-node-requirement';
+
+type CliModule = { runCli: (args: string[]) => Promise<void> };
+
+export const startCli = async ({
+ args,
+ nodeVersion,
+ loadCli,
+}: {
+ args: string[];
+ nodeVersion: string;
+ loadCli: () => Promise<CliModule>;
+}) => {
+ const isNodeSupported =
+ checkNodeRequirement({ version: nodeVersion, range: engines.node }) ===
+ 'satisfied';
+ let cli: CliModule;
+
+ try {
+ cli = await loadCli();
+ } catch (error) {
+ if (isNodeSupported) {
+ throw error;
+ }
+
+ process.stderr.write(
+ `twenty requires Node.js ${engines.node}; this is Node.js ${nodeVersion}. Switch to a supported Node.js version, then try again.\n`,
+ );
+ process.exitCode = 1;
+
+ return;
+ }
+
+ await cli.runCli(args);
+};