Import AWS SDK into Angular 2 app

I am trying to use the AWS SDK in my Angular 2 application and I am pretty stuck. Here are the steps I took:

  • Installed aws sdk in my Angular 2 application using npm install aws-sdk
  • Installed types with npm install --save-dev @types/node
  • Trying to enable AWS modules in several ways in my Angular 2 service: declare var AWS: any; , import AWS = require('aws-sdk'); and finally import * as AWS from 'aws-sdk'; .

when I try to use the first and third type of import, I do not get a transpiler error until I try to access the library in the AWS object, i.e. AWS.config.region = 'us-west-2 gives me an error ';' expected. ';' expected. . And when I try to use the second method, I get an error:

Import destination cannot be used when configuring ECMAScript 2015 modules. Consider using 'import * as ns from "mod", "import {a} from" mod "," import d from "mod" or another module format.)

Did I miss a step? I looked at the github project https://github.com/awslabs/aws-cognito-angular2-quickstart/blob/master/src/app/service/aws.service.ts , but their project does not explain how they imported the SDK.

+4
source share
2 answers

You need:

 npm install aws-sdk -S 

Then make sure you have this type installed (if you are not sure, then just run the code below):

 npm install --save-dev @types/node 

Now in your src folder you will find tsconfig.app.ts (make sure you are not confused with tsconfig.ts in the root directory).

Type string

empty and looks like this: "types": [] . You need to edit it. Tsconfig.app.ts should look like this after adding "types": ["node"] :

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

Now you can just

 import * as AWS from 'aws-sdk'; 
+6
source

I am currently working with AWS with angular as my interface, and use the same angular cognito quickstart project for my authentication and authorization flow.

For installation, I used npm install aws-sdk --save

aws-sdk is under 'dependencies in my package.json :

 "dependencies": { ....... "aws-sdk": "^2.41.0", ....... } 

If you are using angular cli, make sure you include aws-sdk in your angular-cli.json

After that I used declare var AWS: any; And I was able to use the library without any problems.

+2
source

Source: https://habr.com/ru/post/1245083/


All Articles