Amplify-js: Importing aws-exports.js using typescript generates error.

Created on 2 Feb 2018  路  7Comments  路  Source: aws-amplify/amplify-js

According to the documentation, I should/could import the aws-exports module after downloading the proper file from mobile HUB, and then use that configuration to initialize Amplify's configuration.

However I am having many troubles doing so. To start, I need to set "allowJs":true in tsconfig.

Then I import everything as specified in docs.

import Amplify from "aws-amplify";
import aws_exports from '../../aws-exports';

then use it

ionViewDidLoad() {
    console.log('ionViewDidLoad home');
    Amplify.configure(aws_exports);
  }

But when I do, i get this error: Cannot write file '<path>/aws-exports.js' because it would overwrite input file.
If I dont set allowJS=true I get this error:

 Module '../../aws-exports' was resolved to 
        '<path>/aws-exports.js', but '--allowJs' is not set.

Am I doing something wrong? Is Amplify supposed to be configured in a typescript application, like Ionic application?

Apart from this which I am not sure if it is a bug or improper configuration, any help/link/example would be greatly appreciated.

feature-request

Most helpful comment

Add new file
aws-exports.d.ts
export = awsmobile; declare const awsmobile: any;

All 7 comments

as a reference (I don't know if it is correct or a workaround) I renamed aws-export.js to .ts and compiled it. After that no allowJs was needed.

@superandrew that is exactly what I do for now (rename to .ts for the compiler). We are actively working on a fix for this for TS specifically in the awsmobile-cli, but what you are doing is the correct workaround for now.

@superandrew if you don't like to change filename. Use 'require' instead of 'import' works for me. Just be aware when require you get { default } so need to get .default from require.

const aws_exports = require('../../aws-exports').default;

Thanks @richardzcode

You can also just rename aws-exports.js to aws-exports.ts and import './aws-exports';

Still no way to generate aws-exports.ts ? We're nearly late 2019!!

Add new file
aws-exports.d.ts
export = awsmobile; declare const awsmobile: any;

Was this page helpful?
0 / 5 - 0 ratings