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

gracefully handle error when @types/node is not installed #1422

Merged
merged 1 commit into from Aug 8, 2021
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
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