aws-sdk-js: Typescript error: Cannot find name 'Buffer'/'http'/'https'

Hi!

First, I want to thank you for this SDK, it will be very useful for us!

I have some errors when building the javascript bundle for my application (React/Typescript/Webpack). It seems to work anyway (or maybe I have not used it enough yet), but I am not comfortable building our app with errors 😃.

Here is an extract of the errors:

Hash: 6824b5df64cd8f5d85c3
Version: webpack 1.14.0
Time: 4139ms
          Asset     Size  Chunks             Chunk Names
    frontend.js  3.63 MB       0  [emitted]  main
frontend.js.map  5.28 MB       0  [emitted]  main
    + 617 hidden modules

ERROR in /Users/rmarenco/Dev/taiga/frontend/node_modules/aws-sdk/clients/wafregional.d.ts
(416,39): error TS2304: Cannot find name 'Buffer'.

...

ERROR in /Users/rmarenco/Dev/taiga/frontend/node_modules/aws-sdk/lib/config.d.ts
(1,34): error TS2307: Cannot find module 'http'.

ERROR in /Users/rmarenco/Dev/taiga/frontend/node_modules/aws-sdk/lib/config.d.ts
(2,35): error TS2307: Cannot find module 'https'.

And you can find the full errors on this gist => https://gist.github.com/remimarenco/c51c3170fbfd8d548e6fd2fe4542cdb1

I am also including my configuration files in the gist, in case they could be useful 😃. The architecture of my application is like this:

taiga/

  • frontend/
    • node_modules/ <= All my frontend dependencies
    • src/ <= All my .tsx
    • package.json
  • node_modules/ <= All my backend and dev dependencies
  • taiga2/
    • static/ <= Folder where webpack store the outputs
  • package.json
  • tsconfig.json
  • webpack.config.js

Do you think you know what is not correct in my configuration? Or is it on the sdk side + webpack (and/or typescript)?

Thanks a lot!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 36 (6 by maintainers)

Most upvoted comments

@pvamshi So, I was able to reproduce your issue. You actually need to add "types": ["node"] to the tsconfig.app.json file that the angular-cli creates in the src directory. By default, types is an empty array.

Based on these TypeScript docs:

Specify “types”: [] to disable automatic inclusion of @types packages.

Since types is empty, it is excluding the node typings that you installed via npm install @types/node.

I had the same issue as @pvamshi. @chrisradek’s solution tsconfig.app.json worked for me, but because that file extends ../tsconfig.json you can also just delete "types": ["node"] if you followed his previous instructions re: tsconfig.json. Thanks for all your hard work troubleshooting this.

1nvcp6

@remimarenco TypeScript is complaining because some node environment types are needed. This is a limitation for now that we might be able to get around by stubbing those interfaces in the future.

I admit it’s odd to have to include node typings for a front-end project, but can you try installing the environment typings to see if it gets around your issue? npm install --save-dev @types/node

Updated tsconfig.app.json inside src folder, it works for me, thanks!

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": ["node"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

I found a work-around, I have to dig deeper to see if the issue is related to just ts-loader or not.

In your tsconfig.json file, you can add "types": ["node"] as one of your compilerOptions. Then, also make sure to install @types/node at the same level as your tsconfig.json file. If I followed your gist correctly, that would be your top-level package.json, the one that’s a sibling of your tsconfig.json file.

I have same problem. I am using angular-cli stable with Angular 4. I added "types":["node"], but the problem still exists. Any help is appreciated.

I also had to add

"typeRoots": [
      "../node_modules/@types"
    ]

After changing tsconfig.app.json, it worked for me. Changing tsconfig.json hasn’t solved this issue.

Based on the last comments, it sounds like this issue is resolved: there are some Angular-specific hurdles to using the dependent @types/node module, but with appropriate configuration everything works as expected.

Please let me know if I’m mistaken and there’s something more the SDK should be doing here.

For anybody using the ionic framework, and wanting to use the aws-sdk with typings. Its a bit different than what explained above:

  • npm install --save-dev @types/node
  • copy ./node_modules/@types/node/index.d.ts ; to ./src/providers
  • npm uninstall --save-dev @types/node

then you should be able to build if you still get errors, go into the typings file you just copied and clear the errors, there should be a reference import on line 30, that you can comment out, and on line 2381: class URL says its duplicate, just rename it, and URLSearchParams below.

Hi @MissAnichka, did you have this error message: ERROR in /Users/j/Projects/commercial/node_modules/aws-amplify/lib/PubSub/Providers/MqttOverWSProvider.d.ts (1,23): Cannot find type definition file for ‘paho-mqtt’.

EDIT: fix is npm i –save-dev @types/zen-observable npm i –save-dev @types/paho-mqtt

It doesn’t make any sense, but on Ionic you have to add this, even if it doesn’t make sense. under compiler options

    "typeRoots": [
      "node_modules/@types"
    ]

I was able to make it work by taking out the types key in tsconfig.app.json. Putting back in the empty list made it fail again, so it seems like the non-existence of that key also will fix the issue.

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015"
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

This worked for me as well. May I suggest, you could add this as an official step to take in the README.md. Based on the feedback here, it seems to help anyone adding aws-sdk to an angular-cli based project.