From 3f1cb7b8e60addfaab16c0694927346fec24eb29 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 25 Mar 2022 07:44:42 -0800 Subject: [PATCH] chore: only restart esm on Node 16+ (#12955) --- packages/playwright-test/src/cli.ts | 4 ++++ packages/playwright-test/src/loader.ts | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/playwright-test/src/cli.ts b/packages/playwright-test/src/cli.ts index 27b6641539..e6764d0963 100644 --- a/packages/playwright-test/src/cli.ts +++ b/packages/playwright-test/src/cli.ts @@ -243,6 +243,10 @@ async function launchDockerContainer(): Promise<() => Promise> { } function restartWithExperimentalTsEsm(configFile: string | null): boolean { + const nodeVersion = +process.versions.node.split('.')[0]; + // New experimental loader is only supported on Node 16+. + if (nodeVersion < 16) + return false; if (!configFile) return false; if (process.env.PW_DISABLE_TS_ESM) diff --git a/packages/playwright-test/src/loader.ts b/packages/playwright-test/src/loader.ts index 755ec3ba3c..b2564a188e 100644 --- a/packages/playwright-test/src/loader.ts +++ b/packages/playwright-test/src/loader.ts @@ -252,8 +252,13 @@ export class Loader { if (didYouMean?.endsWith('.ts')) throw errorWithFile(file, 'Cannot import a typescript file from an esmodule.'); } - if (error.code === 'ERR_UNKNOWN_FILE_EXTENSION' && error.message.includes('.ts')) - throw errorWithFile(file, 'Cannot import a typescript file from an esmodule.'); + if (error.code === 'ERR_UNKNOWN_FILE_EXTENSION' && error.message.includes('.ts')) { + throw errorWithFile(file, `Cannot import a typescript file from an esmodule.\n${'='.repeat(80)}\nMake sure that: + - you are using Node.js 16+, + - your package.json contains "type": "module", + - you are using TypeScript for playwright.config.ts. +${'='.repeat(80)}\n`); + } if (error instanceof SyntaxError && error.message.includes('Cannot use import statement outside a module')) throw errorWithFile(file, 'JavaScript files must end with .mjs to use import.');