Cdktf encountered an error while synthesizing

I am getting started with cdktf and using this example https://github.com/hashicorp/terraform-cdk/tree/main/examples/typescript/google

I am getting the following error. Any hints on what would be causing it?

2023-08-15T00:21:32.020] [ERROR] default - Error: Cannot find module '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/lib/index.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:409:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:651:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/home/user/code/terraform-cdk/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/usr/local/home/user/code/terraform-cdk/examples/typescript/main.ts:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module.m._compile (/usr/local/home/user/code/terraform-cdk/node_modules/ts-node/src/index.ts:1618:23) {
  code: 'MODULE_NOT_FOUND',
  path: '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/package.json',
  requestPath: 'cdktf'
ERROR: cdktf encountered an error while synthesizing

Synth command: npx ts-node main.ts
Error:         non-zero exit code 1

Command output on stderr:

    Error: Cannot find module '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/lib/index.js'. Please verify that the package.json has a valid "main" entry
        at tryPackage (node:internal/modules/cjs/loader:409:19)
        at Function.Module._findPath (node:internal/modules/cjs/loader:651:18)
        at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
        at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/home/user/code/terraform-cdk/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
        at Function.Module._load (node:internal/modules/cjs/loader:885:27)
        at Module.require (node:internal/modules/cjs/loader:1105:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (/usr/local/home/user/code/terraform-cdk/examples/typescript/main.ts:4:1)
        at Module._compile (node:internal/modules/cjs/loader:1218:14)
        at Module.m._compile (/usr/local/home/user/code/terraform-cdk/node_modules/ts-node/src/index.ts:1618:23) {
      code: 'MODULE_NOT_FOUND',
      path: '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/package.json',
      requestPath: 'cdktf'
    }

Can you provide a bit more context? what commands did you run after cloning the repository to get to this error message as Iā€™m unable to replicate it?

Followed the guide https://github.com/hashicorp/terraform-cdk/blob/main/examples/typescript/google/Readme.md

yarn install
cdktf get
cdktf synth

1 Like
~/code/terraform-cdk/examples/typescript/google$ yarn install
yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "@typescript-eslint/eslint-plugin > tsutils@3.21.0" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
warning " > cdktf@0.0.0" has unmet peer dependency "constructs@^10.0.25".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > cdktf-cli > ink-select-input@4.2.1" has incorrect peer dependency "react@^16.5.2 || ^17.0.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > cdktf > jsii-docgen@9.1.2" has unmet peer dependency "jsii-rosetta@~5.0.14 || ~5.1.2".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-aws-prebuilt > @cdktf/provider-aws@0.0.111" has incorrect peer dependency "constructs@^3.0.4".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-azure-service-bus-queue-trigger > @cdktf/provider-azurerm@9.0.6" has incorrect peer dependency "cdktf@^0.17.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-documentation > @cdktf/provider-dnsimple@0.1.8" has incorrect peer dependency "cdktf@^0.13.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-documentation > @cdktf/provider-github@6.0.2" has incorrect peer dependency "cdktf@^0.15.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-documentation > @cdktf/provider-kubernetes@4.0.2" has incorrect peer dependency "cdktf@^0.14.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > @examples/typescript-documentation > @cdktf/provider-random@4.0.0" has incorrect peer dependency "cdktf@^0.14.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > cdktf-cli > @skorfmann/ink-confirm-input > ink-text-input@4.0.3" has incorrect peer dependency "react@^16.5.2 || ^17.0.0".
warning "workspace-aggregator-579faa79-99e0-41d5-90bd-7559913eb6b4 > cdktf-cli > ink > react-reconciler@0.26.2" has incorrect peer dependency "react@^17.0.2".
[4/4] Building fresh packages...
Done in 40.42s.
~/code/terraform-cdk/examples/typescript/google$ cdktf get
Generated typescript constructs in the output directory: .gen
~/code/terraform-cdk/examples/typescript/google$ cdktf synth

ā   Synthesizing
[2023-08-15T14:41:55.957] [ERROR] default - Error: Cannot find module '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/lib/index.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:409:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:651:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/home/user/code/terraform-cdk/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/usr/local/home/user/code/terraform-cdk/examples/typescript/main.ts:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module.m._compile (/usr/local/home/user/code/terraform-cdk/node_modules/ts-node/src/index.ts:1618:23) {
  code: 'MODULE_NOT_FOUND',
  path: '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/package.json',
  requestPath: 'cdktf'
ERROR: cdktf encountered an error while synthesizing

Synth command: npx ts-node main.ts
Error:         non-zero exit code 1

Command output on stderr:

    Error: Cannot find module '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/lib/index.js'. Please verify that the package.json has a valid "main" entry
        at tryPackage (node:internal/modules/cjs/loader:409:19)
        at Function.Module._findPath (node:internal/modules/cjs/loader:651:18)
        at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
        at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/home/user/code/terraform-cdk/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
        at Function.Module._load (node:internal/modules/cjs/loader:885:27)
        at Module.require (node:internal/modules/cjs/loader:1105:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (/usr/local/home/user/code/terraform-cdk/examples/typescript/main.ts:4:1)
        at Module._compile (node:internal/modules/cjs/loader:1218:14)
        at Module.m._compile (/usr/local/home/user/code/terraform-cdk/node_modules/ts-node/src/index.ts:1618:23) {
      code: 'MODULE_NOT_FOUND',
      path: '/usr/local/home/user/code/terraform-cdk/node_modules/cdktf/package.json',
      requestPath: 'cdktf'
    }

Solved by running yarn install at root project dir level