aws-sdk-js: Cannot get aws-sdk working with Angular universal

Hi,

We use aws-sdk with an Angular 5 application. Until now everything worked great.

We are trying to add server side rendering using angular-universal. When trying to build the server application, like this : https://github.com/angular/angular-cli/wiki/stories-universal-rendering#building-the-bundle , we get these errors :

ERROR in node_modules/aws-sdk/lib/http_response.d.ts(1,25): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/lib/http_response.d.ts(14,18): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/acm.d.ts(108,37): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/acm.d.ts(110,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/acm.d.ts(401,32): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/apigateway.d.ts(1121,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/clouddirectory.d.ts(1469,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/cloudsearchdomain.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/cloudsearchdomain.d.ts(42,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/cloudtrail.d.ts(141,28): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/codecommit.d.ts(1539,22): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/cognitoidentityserviceprovider.d.ts(2561,31): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/directconnect.d.ts(992,28): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/dms.d.ts(432,35): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/lib/dynamodb/document_client.d.ts(2,25): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/lib/dynamodb/document_client.d.ts(93,30): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/lib/dynamodb/document_client.d.ts(274,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/dynamodb.d.ts(466,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/dynamodbstreams.d.ts(92,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/ec2.d.ts(3006,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/ecr.d.ts(767,31): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/firehose.d.ts(156,22): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/lib/services/glacier.d.ts(10,28): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/glacier.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/glacier.d.ts(1313,24): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/iam.d.ts(1112,32): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/iam.d.ts(3106,35): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/iotdata.d.ts(73,30): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/iotdata.d.ts(74,25): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/kinesis.d.ts(229,22): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/kms.d.ts(328,32): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/kms.d.ts(962,31): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/lambda.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/lambda.d.ts(364,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/lambda.d.ts(365,28): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/lexruntime.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/lexruntime.d.ts(35,28): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/polly.d.ts(8,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/polly.d.ts(70,29): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/rekognition.d.ts(1135,27): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/ses.d.ts(1513,32): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/sns.d.ts(275,24): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/sqs.d.ts(216,24): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/ssm.d.ts(4564,48): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/support.d.ts(336,22): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/waf.d.ts(604,39): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/wafregional.d.ts(648,39): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/mobile.d.ts(106,26): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/mediastoredata.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/mediastoredata.d.ts(199,29): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/appsync.d.ts(240,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/kinesisvideoarchivedmedia.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/kinesisvideoarchivedmedia.d.ts(121,25): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/kinesisvideomedia.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/kinesisvideomedia.d.ts(53,25): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/sagemakerruntime.d.ts(24,26): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/lib/config.d.ts(1,34): error TS2307: Cannot find module 'http'.
node_modules/aws-sdk/lib/config.d.ts(2,35): error TS2307: Cannot find module 'https'.
node_modules/aws-sdk/lib/request.d.ts(1,25): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/lib/request.d.ts(132,45): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/s3.d.ts(10,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/s3.d.ts(777,22): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/s3.d.ts(1131,42): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/s3.d.ts(3473,32): error TS2304: Cannot find name 'Buffer'.

Please note that the client application still builds ok.

Thanks for your help.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Adding "types":["node"] to tsconfig.app.json file worked for me.

Hi @achelkia Maybe you can try remove the "types": [] from the tsconfig.server.json? Our readme does mention you need to either add node to type list or remove the entry. Another issue #1271 provides a workaround for similar issue.

Hi @AllanFly120, thanks for the answer. That was it, removing "types": [] or adding node did it for me. Thanks again.

In my case. I used global types and “types”:[“node”] inside tsconfig.app.json was overwriting “typeRoots” value inside tsconfig json. So the solution is as follows:

  1. 1 remove “types” from tsconfig.app.json
  2. Ensure that global types are declared inside tsconfig json as follows “typeRoots”: [“node_modules/@types”]
  3. IMPORTANT. Add browser: true settings. In my case it was inside polyfils.ts as (window as any).process = { browser: true //IMPORTANT for aws-sdk };

Angular 6, First I got the error while compiling :

ERROR in node_modules/aws-sdk/clients/acm.d.ts(133,37): error TS2304: Cannot find name ‘Buffer’

So as suggested in this post: I added "types": ["node"] to tsconfig.app.json

Project Compiled successfully.

But then it did open on browser. Got the error in console:

Uncaught ReferenceError: global is not defined

So as suggested in other issue on https://github.com/aws-amplify/amplify-js/issues/678 added following line

// Add global to window, assigning the value of window itself.
(window as any).global = window;

got my app working

I see you did not install @types/node. Our type definition uses Buffer, stream types which are declared in @type/node. Just try npm -i -d @types/node

I have the same problem but with angular 6, and even with these changes that comment can not solve the problem, I hope you can help me thanks! screenshot_2