Skip to content

Commit

Permalink
fix(core): remove logic to reload process with esm loader for Node 18 (
Browse files Browse the repository at this point in the history
…#21623)

(cherry picked from commit 7086f57)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Feb 5, 2024
1 parent f7ed73d commit 5a8a56d
Showing 1 changed file with 0 additions and 54 deletions.
54 changes: 0 additions & 54 deletions packages/nx/bin/init-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 5a8a56d

Please sign in to comment.