Amplify-js: ReferenceError: fetch is not defined

Created on 20 Jun 2018  路  8Comments  路  Source: aws-amplify/amplify-js

Do you want to request a feature or report a bug?

bug

What is the current behavior?

ReferenceError: fetch is not defined
    at Client.request (/Users/user/sbox/amplify/node_modules/amazon-cognito-identity-js/lib/Client.js:54:5)
    at /Users/user/sbox/amplify/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:318:21
    at AuthenticationHelper.getLargeAValue (/Users/user/sbox/amplify/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:85:7)
    at CognitoUser.authenticateUserDefaultAuth (/Users/user/sbox/amplify/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:296:26)
    at CognitoUser.authenticateUser (/Users/user/sbox/amplify/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:257:19)
    at /Users/user/sbox/amplify/node_modules/aws-amplify/lib/Auth/Auth.js:334:18
    at new Promise (<anonymous>)
    at AuthClass.signIn (/Users/user/sbox/amplify/node_modules/aws-amplify/lib/Auth/Auth.js:333:16)
    at Object.<anonymous> (/Users/user/sbox/amplify/index.js:26:6)
    at Module._compile (module.js:652:30)

What is the expected behavior?

No errors

Example

'use strict';

const Amplify = require( 'aws-amplify' )
    .default;
const Auth = Amplify.Auth;

Amplify.configure( {
    Auth: {
        // REQUIRED - Amazon Cognito Identity Pool ID
        identityPoolId: 'us-east-2:...',
        // REQUIRED - Amazon Cognito Region
        region: 'us-east-2',
        // OPTIONAL - Amazon Cognito User Pool ID
        // userPoolId: 'us-east-', // username
        userPoolId: 'us-east-', // email & password
        // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        // userPoolWebClientId: '...', // username
        userPoolWebClientId: '...', // email & password
        // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
        mandatorySignIn: true,
    }
} );

Auth.signIn('[email protected]', 'kdasfjcbkKJB87678')
    .then(user => console.log(user))
    .catch(err => console.error(err));

Why error?

investigating

Most helpful comment

Same here https://github.com/aws/aws-amplify/issues/876

global.fetch = require('node-fetch')

All 8 comments

Same here https://github.com/aws/aws-amplify/issues/876

global.fetch = require('node-fetch')

@wzup : simply do require('node-fetch') before you load amplify. it works for me

global.fetch = require('node-fetch')

Property 'fetch' does not exist on type 'Global'.

@Mulperi how could we use on angular app?

@devotebest do you have issues with angular 6? Here are instructions on how to use it.

@devotebest You can try with whatwg-fetch as mentionned in this post.

@Mulperi

npm install --save @types/node-fetch

import 'node-fetch'
global['fetch'] = fetch;

@Mulperi

npm install --save @types/node-fetch

import 'node-fetch'
global['fetch'] = fetch;

Almost there...

import * as fetch from 'node-fetch';
global['fetch'] = fetch;

Was this page helpful?
0 / 5 - 0 ratings