I am generating some code using ts-morph
and I want to parse a file in one of my dependencies.
I tried using ts.resolveModuleName
but this does not search in the parent directories:
const x = ts.resolveModuleName(
"@pulumi/aws",
"",
project.compilerOptions.get(),
project.getModuleResolutionHost(),
);
console.log(x);
logs:
{
resolvedModule: undefined,
failedLookupLocations: [
'node_modules/@pulumi/aws/package.json',
'node_modules/@pulumi/aws.ts',
'node_modules/@pulumi/aws.tsx',
'node_modules/@pulumi/aws.d.ts',
'node_modules/@pulumi/aws/index.ts',
'node_modules/@pulumi/aws/index.tsx',
'node_modules/@pulumi/aws/index.d.ts',
'node_modules/@types/pulumi__aws/package.json',
'node_modules/@types/pulumi__aws.d.ts',
'node_modules/@types/pulumi__aws/index.d.ts',
'node_modules/@pulumi/aws/package.json',
'node_modules/@pulumi/aws.js',
'node_modules/@pulumi/aws.jsx',
'node_modules/@pulumi/aws/index.js',
'node_modules/@pulumi/aws/index.jsx'
],
affectingLocations: [
'/Users/jason/src/bottech/pulumix/packages/pulumix-aws/package.json'
],
resolutionDiagnostics: undefined,
node10Result: undefined
}
compilerOptions.moduleResolution
is NodeNext
.
I want it to also look in /Users/jason/src/bottech/pulumix/node_modules
like it does when using tsc
.
What is it that I am missing to get it to behave like the normal resolution algorithm?
It works if you pass a proper path for the containingFile
. For example:
ts.resolveModuleName(
"@pulumi/aws",
__filename,
project.compilerOptions.get(),
project.getModuleResolutionHost(),
);
You can see the resolution by a) enabling the traceResolution
option and b) ensuring that the host has defined the trace
method:
ts.resolveModuleName(
"@pulumi/aws",
__filename,
{ ...project.compilerOptions.get(), traceResolution: true },
{ ...project.getModuleResolutionHost(), trace: console.trace },
);