Kerruba
Kerruba

Reputation: 2009

AWS Javascript SDK v3 - Typescript doesn't compile due to error TS2304: Cannot find name 'ReadableStream'

I'm trying to build my project that uses the AWS Javascript SDK v3. Here my tsconfig.json

{
  "compilerOptions": {
    "target":"ES2020",
    "module": "commonjs",
    "lib": ["es2020"],
    "outDir": "dist",
    "resolveJsonModule": true,
  },
  "exclude": [
    "coverage",
    "node_modules",
    "dist",
    "tests"
  ]
}

And here an example of the build error I get (I've strip out some of the output for brevity sake):

node_modules/@aws-sdk/client-s3/types/models/models_1.d.ts:727:23 - error TS2304: Cannot find name 'ReadableStream'.

node_modules/@aws-sdk/client-s3/types/models/models_1.d.ts:727:40 - error TS2304: Cannot find name 'Blob'.

node_modules/@aws-sdk/util-dynamodb/types/models.d.ts:19:86 - error TS2304: Cannot find name 'File'.

I don't understand why this is giving me such an issue, even if I have installed the @types/node module to support node typing

Upvotes: 37

Views: 15510

Answers (4)

It is always recommended to use the aws-sdk v3 in combination with Node 18. If you do not want to include DOM, you can specify Node 18 in the tsconfig.

{
    //...
    "compilerOptions": {
        "target":"ES2022",
        "module": "commonjs",
        "lib": ["es2022"],
        //...
    }
    //...
}

Additionally, you should use @types/node in version 18:

npm i -D @types/node@18

Upvotes: 3

tobyjaguar
tobyjaguar

Reputation: 1

I used the declare global solution but I had to declare one of the types as 'any' to get around a class that implemented the Storage class:

export declare class UniversalStorage implements Storage {

therefore instead of unknown, I just made all the offenders type 'any'

Upvotes: 0

Walter Barbagallo
Walter Barbagallo

Reputation: 251

Without including the dom lib in the tsconfig.json

// src/@types/dom.ts (or any file included in the tsc compilation)

export {}

declare global {
  type ReadableStream = unknown
  type Blob = unknown
}

Upvotes: 18

Kerruba
Kerruba

Reputation: 2009

Turned out that in order for typescript to find the Blob, File etc. names I had to add the dom entry to the lib in my tsconfig.json. Here my final tsconfig, which allow me to build the project correctly

{
  "compilerOptions": {
    "target":"ES2020",
    "module": "commonjs",
    "lib": ["es2020", "dom"],
    "outDir": "dist",
    "resolveJsonModule": true,
  },
  "exclude": [
    "coverage",
    "node_modules",
    "dist",
    "tests"
  ]
}

Upvotes: 76

Related Questions