MCPcopy
hub / github.com/TypeStrong/ts-node / requireResolveNonCached

Function requireResolveNonCached

src/bin.ts:744–764  ·  view source on GitHub ↗

* require.resolve an absolute path, tricking node into *not* caching the results. * Necessary so that we do not pollute require.resolve cache prior to installing require.extensions * * Is a terrible hack, because node does not expose the necessary cache invalidation APIs * https://stackoverflow.

(absoluteModuleSpecifier: string)

Source from the content-addressed store, hash-verified

742 * https://stackoverflow.com/questions/59865584/how-to-invalidate-cached-require-resolve-results
743 */
744function requireResolveNonCached(absoluteModuleSpecifier: string) {
745 // node <= 12.1.x fallback: The trick below triggers a node bug on old versions.
746 // On these old versions, pollute the require cache instead. This is a deliberate
747 // ts-node limitation that will *rarely* manifest, and will not matter once node 12
748 // is end-of-life'd on 2022-04-30
749 const isSupportedNodeVersion = versionGteLt(process.versions.node, '12.2.0');
750 if (!isSupportedNodeVersion) return require.resolve(absoluteModuleSpecifier);
751
752 const { dir, base } = parsePath(absoluteModuleSpecifier);
753 const relativeModuleSpecifier = `./${base}`;
754
755 const req = createRequire(
756 join(dir, 'imaginaryUncacheableRequireResolveScript')
757 );
758 return req.resolve(relativeModuleSpecifier, {
759 paths: [
760 `${guaranteedNonexistentDirectoryPrefix}${guaranteedNonexistentDirectorySuffix++}`,
761 ...(req.resolve.paths(relativeModuleSpecifier) || []),
762 ],
763 });
764}
765
766/**
767 * Evaluate an [eval] or [stdin] script

Callers 1

getProjectSearchDirFunction · 0.85

Calls 2

versionGteLtFunction · 0.90
createRequireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…