Skip to content

bug(aws-lambda-nodejs): esbuild preCompilation not finding local tsc #19242

@derdeka

Description

@derdeka

What is the problem?

The esbuild preCompilation option requires typescript to be installed globally and does not find a local (project) installation.

Reproduction Steps

With no global typescript installation, but local typescript dev dependency and the following configuration:

new NodejsFunction(scope, id, {
  ...
  bundling: {
    preCompilation: true
  }
})

What did you expect to happen?

tsc command should be found when typescript is locally installed.

What actually happened?

The command "undefined" is either misspelled or could not be found.

CDK CLI Version

2.15.0

Framework Version

2.15.0

Node.js Version

14.15.3

OS

Windows 10

Language

Typescript

Language Version

TypeScript (4.3.5)

Other information

Searching for command tsc is happening here:

Bundling.tscInstallation = Bundling.tscInstallation ?? PackageInstallation.detect('tsc');

// Check local version first
const version = tryGetModuleVersionFromRequire(module);
if (version) {
return {
isLocal: true,
version,
};
}

It tries to find the local installation using require('tsc/package.json') which can not be found. The package name has to be typescript instead of tsc to detect it correctly.

export function tryGetModuleVersionFromRequire(mod: string): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(`${mod}/package.json`).version;
} catch (err) {
return undefined;
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions