Error message: "Unable to locate the module 'typescript' in AWS CDK"

After using a mono repo for nearly a year, I encountered a hard drive failure that forced me to reinstall my OS. Now, every time I attempt to use the CDK, I encounter the following error:

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 issue is specific to the cdk synth|diff|deploy command. Running tests does not pose any problems, which added to my confusion as I investigated snapshots.

Initially, I suspected a PATH-related problem, but I couldn't understand why it would arise now. Although I previously used .husky, I have since developed my own system. I mention this in case it holds any significance.

The root of the issue might lie in node_module resolution, particularly due to the presence of a node_modules directory both within this project and its parent. This project utilizes a mono repo with lerna. It's worth noting that this isn't an issue with lerna, as the problem arises only when executing cdk synth|diff|deploy. Even a simple task like creating a bucket triggers the same error.

I suspect the problem revolves around node_modules resolutions. The execution doesn't align with where my tsconfig resides, while all other scripts function correctly without issues. The inconsistency puzzles me, especially given that I've successfully tested four other binaries (jest, prettier, eslint, and tsc), all of which run smoothly. The hurdle appears isolated to cdk.

Lerna plays a role in managing the mono repo setup.

The devDependencies list includes:

  "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",
    ...
},

Despite installing Typescript globally--a departure from my usual preference for local installs across the board--the issue persists.

Running tsc --showConfig confirmed that the tsconfig settings align with expectations:

{
    "compilerOptions": {
        ...
    },
    ...
}

Examining the npm log revealed:

...

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)

In an attempt to resolve the issue, even resorting to globally installing typescript and the cdk didn't yield success.

Your insights and suggestions are highly appreciated. I've dedicated an entire day trying to troubleshoot this to no avail.

Project Layout:

├── accounts
│   ├── olympusat-development-dev
│   ├── README.md
│   └── us-east-1/
...

Answer №1

Dealing with the same problem, I encountered issues with my build process.

Luckily, installing ts-node globally helped resolve the issue for me.

npm install -g typescript
npm install -g ts-node
npm install -g aws-cdk

Answer №2

I successfully implemented a solution to resolve the issue at hand, although I am puzzled as to why this particular approach was necessary after having no problems for over a year using the current setup. It's worth noting that other developers have been utilizing the system without encountering any issues, making the situation even more perplexing.

With a minor adjustment, I managed to rectify the problem. The modification I made was in the cdk.json file:

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

As a result of this change, I am now able to execute all cdk commands effectively.

The reason behind this could be attributed to my preference for local installations over global ones. By keeping all packages within the project directory and specifying their location when required, I believe I have resolved the issue previously masked by global installations. The discrepancy in experiences with other developers could potentially stem from differing installation methods (global vs. local).

During the bootstrapping process, I ensure to delete all node_modules directories. The initial problem stemmed from the reliance on the child node_modules directory rather than the parent directory expected by Lerna. By explicitly specifying the path

../../../node_modules/.bin/ts-node
, I am confident that the correct node_modules directory is now being utilized.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes the error 404 in bcrypt to occur during the npm install process?

When attempting to execute sudo npm install in my project, I encountered the following error message: Unable to download (404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.3/bcrypt_lib-v1.0.3-node-v64-linux-x64.tar.gz node-pre-gyp E ...

get a duplicate of an object

Is this the proper method for creating a duplicate of an object? class ObjectWrapper { private _obj; /*** * Copy object passed as argument to this._obj */ constructor (_obj: Object) { this._obj = _obj; } /** Return copy of this._ ...

Why am I encountering this rendering issue when passing data to the ReactTable component?

The following code snippet represents the parent component containing an array of columns and data. const TransactionTable = () => { const columns = useMemo( () => [ { Header: 'DATE/TIME', accessor: &apos ...

When trying to reference a vanilla JavaScript file in TypeScript, encountering the issue of the file not being recognized

I have been attempting to import a file into TypeScript that resembles a typical js file intended for use in a script tag. Despite my efforts, I have not found success with various methods. // global.d.ts declare module 'myfile.js' Within the re ...

Ensure that the value of a variable in the Ionic/Angular template has been properly initialized

I am currently facing an issue: I have an array of blog posts. Some of them have photos, while others do not. I aim to display the first photo if any are set. How can I verify whether the URL value in my array is set? <ion-header> <ion-navbar& ...

What is the solution for resolving the "Directory not empty" error when moving files during a docker build process?

I have successfully created a Dockerfile for a node application: FROM node:8.8 ENV TERM=xterm-color NPM_CONFIG_LOGLEVEL=warn PATH="$PATH:/usr/src/app/node_modules/.bin/" VOLUME ["/logs"] WORKDIR /tmp/node ADD package.json yarn.lock .npmrc ./ RUN yarn inst ...

Footer missing from Tanstack React table

Library Version: "@tanstack/react-table": "^8.2.6", I have been attempting to include footers in my table without success. Despite setting static footer text with a fixed value, I am unable to render any dynamic values similar to how h ...

Issue with unapplied nullable type during export操作

I'm struggling to understand why my nullable type isn't being applied properly Here's an illustration interface Book { name: string; author: string; reference: string; category: string; } async function handleFetch<T>(endpoin ...

Tips for implementing UI properties in React

Utilizing an external UI Library, I have access to a Button component within that library. My goal is to create a customized NewButton component that not only inherits all props from the library Button component but also allows for additional props such as ...

What is the reason behind Typescript raising an error when attempting to compare two boolean variables with different values (true and false)?

In the screenshot below, you can see that we are encountering a peculiar error when attempting to compare a boolean variable with true. This condition will always return 'false' since the types 'false' and 'true' have no over ...

The http post request is functioning properly in postman, but it is not working within the ionic app

I am currently developing an app in ionic v3 within visual studio (Tools for Apache Cordova). On one of the screens in my app, I gather user information and send it to an API. However, I'm encountering an issue with the HTTP POST request that I'm ...

What is the purpose of the tabindex in the MUI Modal component?

Struggling with integrating a modal into my project - it's refusing to close and taking up the entire screen height. On inspection, I found this troublesome code: [tabindex]: outline: none; height: 100% How can I remove height: 100% from the ...

Validating emails using angular material chips

I'm currently working on implementing a form that utilizes input chips to facilitate sending messages to multiple email addresses. While I've made progress in making the form functional, I'm facing difficulties in displaying error messages w ...

What is the reason for requiring that the value type in a map must be uniform?

When using TypeScript, I expect the map type to be either a number or string, but unfortunately, an error is being reported. Click here for the Playground const map: Map<string, string | number> = new Map([ [ '1', &apo ...

Steps for setting up opencv4nodejs on a Windows 10 system

I'm having trouble installing opencv4nodejs. When I run npm install opencv4nodejs, I get the following error message: npm ERR! code 1 npm ERR! path D:\nikunj\Programming\testapp2\node_modules\opencv4nodejs npm ERR! command fai ...

Installing Yarn causes the download of an unconventional directory

Currently, I am facing an issue while trying to install yarn on my Macbook Pro (2017). The installation process seems to be downloading a folder called /react-praktis/ instead of completing successfully. Below is a screenshot for reference: https://i.stac ...

Importing dynamic modules within an Angular 6 library package

Currently, I am developing an Angular library that consists of numerous modules and components. One essential feature is a plugin loading system. The plugin loading mechanism functions under the assumption that the importing project (via npm) will have a ...

Combining Promises in Typescript to create a single Promise

Is there a way for me to return the temp_data object filled with data after using .map? Currently, it always returns undefined because I initialize temp_data as an empty object. But if I don't do this, I can't use LooseObject. Can anyone suggest ...

Setting Docker ENTRYPOINT to NPM: A step-by-step guide

I'm currently working on a Node.js project that utilizes npm scripts. Here is a segment from the package.json: "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build-all": "./node_modules/.bin/webpack --p ...

alert: the package you are trying to uninstall is not currently installed

After downloading and installing Node.js on my Windows system, I decided to uninstall the current version of TypeScript in order to go back to an older version. However, when attempting to do so using either npm uninstall typescript or npm uninstall -g ...