Search code examples
typescriptnpmaws-cdklerna

AWS CDK: Cannot find module 'typescript'


Long story short, I have a mono repo that I have been using for almost a year. I had a hard drive failure and had to reinstall my OS. Now, when I try to do anything with the CDK, I get this error every time.

Cannot find module 'typescript'
Require stack:
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/index.js
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/repl.js
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/bin.js

This only affects the cdk synth|diff|deploy command. I have no issues with running tests which caused me even more confusion as I check snapshots.

At first, I was thinking this has to do with the PATH but I am not sure why this would be an issue now. I did use .husky but have since replaced it with my own system. I only mention this in-case this may be something worth knowing.

This could be an issue with node_module resolution as I have a node_modules directory in this project and the parent. I am using a mono repo with lerna. This isn't a lerna issue as this issue only happens when running cdk synth|diff|deploy. A simple test of just creating a bucket will throw that error.

I'm leaning towards the node_modules resolutions. This is not running where my tsconfig is as all the other scripts are working properly. I'm not sure why CDK is the only one having this issue. I have tested with 4 other binaries (jest, prettier, eslint, and tsc) and they run just fine. The issue is only with cdk.

I am using Lerna for the mono repo.

This is my devDependencies:

  "devDependencies": {
    "@aws-cdk/assert": "1.117.0",
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "@typescript-eslint/eslint-plugin": "^4.4.0",
    "@typescript-eslint/parser": "^4.4.0",
    "aws-cdk": "1.117.0",
    "eslint": "7.19.0",
    "eslint-plugin-jest": "24.3.1",
    "eslint-plugin-import": "2.22.1",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-config-airbnb-typescript": "^9.0.0",
    "eslint-config-prettier": "7.2.0",
    "eslint-plugin-prettier": "3.3.1",
    "eslint-plugin-simple-import-sort": "6.0.1",
    "execa": "^5.0.0",
    "git-url-parse": "^11.4.4",
    "jest": "^26.4.2",
    "jest-extended": "^0.11.5",
    "listr": "^0.14.3",
    "lerna": "^4.0.0",
    "lerna-update-wizard": "^0.17.8",
    "lint-staged": "^10.5.4",
    "prettier": "^2.2.1",
    "ts-jest": "^26.2.0",
    "ts-node": "^9.0.0",
    "typedoc": "^0.20.25",
    "typescript": "~3.9.7",
    "util": "^0.12.3"
  },

I installed Typescript globally to see if this would help (I normally use local installs for everything) but it did not.

I also confirmed that the tsc config is what I expect by running tsc --showConfig:

{
    "compilerOptions": {
        "alwaysStrict": true,
        "declaration": true,
        "experimentalDecorators": true,
        "lib": [
            "es2018"
        ],
        "module": "commonjs",
        "noFallthroughCasesInSwitch": false,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "inlineSourceMap": true,
        "inlineSources": true,
        "removeComments": false,
        "strict": true,
        "strictNullChecks": true,
        "strictPropertyInitialization": false,
        "target": "es2018",
        "typeRoots": [
            "/home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/node_modules/@types"
        ],
        "rootDir": "./src",
        "outDir": "./dist"
    },
    "files": [
        "./src/index.ts",
        "./src/stacks/pipeline-test/index.ts",
        "./src/stacks/pipeline-test/src/pipeline-base.ts",
        "./src/stacks/pipeline-test/src/pipeline-project.ts",
        "./src/stacks/s3-replication-sources/index.ts",
        "./src/stacks/s3-replication-sources/src/s3-replication-sources.ts",
        "./src/stacks/test-base/index.ts",
        "./src/stacks/test-base/src/index.ts",
        "./src/stacks/test-web-bucket/index.ts",
        "./src/stacks/test-web-bucket/src/main.ts"
    ],
    "include": [
        "./src"
    ],
    "exclude": [
        "dist"
    ]
}

npm log:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/node',
1 verbose cli   '/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/npm',
1 verbose cli   'run',
1 verbose cli   'TestBase:diff'
1 verbose cli ]
2 info using npm@6.14.14
3 info using node@v14.17.4
4 verbose run-script [ 'preTestBase:diff', 'TestBase:diff', 'postTestBase:diff' ]
5 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~preTestBase:diff: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
6 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
7 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: unsafe-perm in lifecycle true
8 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: PATH: /home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1/node_modules/.bin:/home/jpsimkins/.local/bin:/home/jpsimkins/bin:/home/jpsimkins/.nvm/versions/node/v14.17.4/bin:/home/jpsimkins/.local/bin:/home/jpsimkins/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
9 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: CWD: /home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1
10 silly lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Args: [
10 silly lifecycle   '-c',
10 silly lifecycle   'aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase'
10 silly lifecycle ]
11 silly lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Returned: code: 1  signal: null
12 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Failed to exec TestBase:diff script
13 verbose stack Error: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff: `aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:400:28)
13 verbose stack     at ChildProcess.<anonymous> (/home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1055:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
15 verbose cwd /home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1
16 verbose Linux 5.11.0-25-generic
17 verbose argv "/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/node" "/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/npm" "run" "TestBase:diff"
18 verbose node v14.17.4
19 verbose npm  v6.14.14
20 error code ELIFECYCLE
21 error errno 1
22 error @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff: `aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase`
22 error Exit status 1
23 error Failed at the @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Environment

 - **CDK CLI Version :**  1.117.0 (build 0047c98)
 - **Module Version :**  1.117.0
 - **Node.js Version :**  v14.17.4
 - **OS               :**  Linux Zeus 5.11.0-25-generic #27~20.04.1-Ubuntu SMP Tue Jul 13 17:41:23 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
 - **Language (Version):** Typescript (Version 3.9.10)

I even tried to install typescript and the cdk globally. This issue still persists.

Any insight is appreciated.. I've spend all day trying to fix this and no luck..

Project Layout:

├── accounts
│   ├── olympusat-development-dev
│   ├── README.md
│   └── us-east-1/
│       ├── cdk.context.json
│       ├── cdk.json
│       ├── cdk.out
│       │   └── ...
│       ├── jest.config.js
│       ├── node_modules
│       │   └── @olympusat-devops
│       │       ├── core -> ../../../../../packages/core
│       │       ├── oly-bastion -> ../../../../../packages/oly-bastion
│       │       ├── oly-efs -> ../../../../../packages/oly-efs
│       │       ├── oly-pipeline -> ../../../../../packages/oly-pipeline
│       │       ├── oly-pipeline-default-image -> ../../../../../packages/oly-pipeline-default-image
│       │       ├── oly-pipeline-templates -> ../../../../../packages/oly-pipeline-templates
│       │       ├── oly-rds-wordpress -> ../../../../../packages/oly-rds-wordpress
│       │       ├── oly-s3-replication-destination -> ../../../../../packages/oly-s3-replication-destination
│       │       ├── oly-s3-replication-source -> ../../../../../packages/oly-s3-replication-source
│       │       ├── oly-vpc -> ../../../../../packages/oly-vpc
│       │       └── oly-web-bucket -> ../../../../../packages/oly-web-bucket
│       ├── package.json
│       ├── README.md
│       ├── src
│       │   ├── index.ts
│       │   └── stacks
│       │       └── ...
│       ├── tests
│       │   └── ...
│       ├── tsconfig.dev.json
│       └── tsconfig.json
├── lerna.json
├── node_modules
│   ├── ...
├── package.json
├── package-lock.json
├── packages
│   ├── core
│   ├── oly-bastion
│   ├── oly-cloudfront-templates
│   ├── oly-efs
│   ├── oly-pipeline
│   ├── oly-pipeline-default-image
│   ├── oly-pipeline-templates
│   ├── oly-rds-wordpress
│   ├── oly-s3-replication-destination
│   ├── oly-s3-replication-source
│   ├── oly-vpc
│   ├── oly-web-bucket
│   └── README.md
├── README.md
└── tsconfig.json

Solution

  • Okay, I was able to get this working but I am not sure why I had to do it this way after it's been working for over a year with the current system. I even have other devs using it with no issue. Very strange.

    To get this working, I had to make a simple change.

    I modified cdk.json to be:

    {
      "app": "../../../node_modules/.bin/ts-node --prefer-ts-exts src/index.ts",
      "context": {
        "@aws-cdk/core:enableStackNameDuplicates": "true",
        "aws-cdk:enableDiffNoFail": "true"
      }
    }
    

    Now I am able to get cdk commands to work.

    This may be due to that I am not installing anything globally. I am requiring all packages be installed locally as this is possible with binaries too if you just specify the location. I assume this worked before as I did originally install globally and they may have been the reason for it to work as it did. Why it works for the other devs, I am unsure as they did not install globally (or so they say).

    Part of my boostrap command is to delete all node_modules directories. The issue was that it was only using the package node_modules directory and not the parent as it should with Lerna. Anyways, by specifying the path ../../../node_modules/.bin/ts-node I am sure that it is working with the correct node_modules (parent) now.