Skip to content

Commit

Permalink
gracefully handle error when @types/node is not installed (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Aug 8, 2021
1 parent dc0fed2 commit 9c1c9ce
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/resolver-functions.ts
Expand Up @@ -137,23 +137,30 @@ export function createResolverFunctions(kwargs: {
);
if (typeDirectiveName === 'node' && !resolvedTypeReferenceDirective) {
// Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
const typesNodePackageJsonPath = require.resolve(
'@types/node/package.json',
{
paths: [configFilePath ?? cwd, __dirname],
}
);
const typeRoots = [resolve(typesNodePackageJsonPath, '../..')];
({ resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(
typeDirectiveName,
containingFile,
{
...config.options,
typeRoots,
},
serviceHost,
redirectedReference
));
let typesNodePackageJsonPath: string | undefined;
try {
typesNodePackageJsonPath = require.resolve(
'@types/node/package.json',
{
paths: [configFilePath ?? cwd, __dirname],
}
);
} catch {} // gracefully do nothing when @types/node is not installed for any reason
if (typesNodePackageJsonPath) {
const typeRoots = [resolve(typesNodePackageJsonPath, '../..')];
({
resolvedTypeReferenceDirective,
} = ts.resolveTypeReferenceDirective(
typeDirectiveName,
containingFile,
{
...config.options,
typeRoots,
},
serviceHost,
redirectedReference
));
}
}
if (resolvedTypeReferenceDirective) {
fixupResolvedModule(resolvedTypeReferenceDirective);
Expand Down

0 comments on commit 9c1c9ce

Please sign in to comment.