31

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

Kerruba
  • 1,779
  • 1
  • 17
  • 25

4 Answers4

66

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"
  ]
}
Kerruba
  • 1,779
  • 1
  • 17
  • 25
  • 10
    This fixed the issue for me. However, my app runs on Nodejs. Will there be any issues adding `dom` to this project—which presumably checks for browser compatibility? Strange that `AWS` couldn't compile against vanilla Nodejs? – sambecker Apr 24 '21 at 18:08
  • 2
    It'll throw more type conflicts and errors if we add 'dom' in our node app. Does anyone know other solution? – user2734550 Jun 11 '21 at 17:31
  • @user2734550 checkout walter-barbagallo solution below – Kerruba Oct 15 '21 at 14:01
17

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
}

0

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'

0

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
sadeq shahmoradi
  • 1,395
  • 1
  • 6
  • 22