From 36b212ceccea35697d48a538cc6edef304c01561 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 5 Feb 2024 13:54:38 -0500 Subject: [PATCH] fix(core): remove logic to reload process with esm loader for Node 18 --- packages/nx/bin/init-local.ts | 54 ----------------------------------- 1 file changed, 54 deletions(-) diff --git a/packages/nx/bin/init-local.ts b/packages/nx/bin/init-local.ts index 94930c59d5cdf..34c96fc3ed690 100644 --- a/packages/nx/bin/init-local.ts +++ b/packages/nx/bin/init-local.ts @@ -13,27 +13,6 @@ import * as Mod from 'module'; */ export function initLocal(workspace: WorkspaceTypeAndRoot) { - // If module.register is not available, we need to restart the process with the experimental ESM loader. - // Otherwise, usage of `registerTsProject` will not work for `.ts` files using ESM. - // TODO: Remove this once Node 18 is out of LTS (March 2024). - if (shouldRestartWithExperimentalTsEsmLoader()) { - const child = require('child_process').fork( - require.resolve('./nx'), - process.argv.slice(2), - { - env: { - ...process.env, - RESTARTED_WITH_EXPERIMENTAL_TS_ESM_LOADER: '1', - }, - execArgv: execArgvWithExperimentalLoaderOptions(), - } - ); - child.on('close', (code: number | null) => { - if (code !== 0 && code !== null) process.exit(code); - }); - return; - } - process.env.NX_CLI_SET = 'true'; try { @@ -215,36 +194,3 @@ function monkeyPatchRequire() { // do some side-effect of your own }; } - -function shouldRestartWithExperimentalTsEsmLoader(): boolean { - // Already restarted with experimental loader - if (process.env.RESTARTED_WITH_EXPERIMENTAL_TS_ESM_LOADER === '1') - return false; - const nodeVersion = parseInt(process.versions.node.split('.')[0]); - // `--experimental-loader` is only supported in Nodejs >= 16 so there is no point restarting for older versions - if (nodeVersion < 16) return false; - // Node 20.6.0 adds `module.register`, otherwise we need to restart process with "--experimental-loader ts-node/esm". - return ( - !require('node:module').register && - moduleResolves('ts-node/esm') && - moduleResolves('typescript') - ); -} - -function execArgvWithExperimentalLoaderOptions() { - return [ - ...process.execArgv, - '--no-warnings', - '--experimental-loader', - require.resolve('ts-node/esm'), - ]; -} - -function moduleResolves(packageName: string) { - try { - require.resolve(packageName); - return true; - } catch { - return false; - } -}