Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): remove logic to reload process with esm loader for Node 18 #21623

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}