question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(fix) fetch is not defined error

See original GitHub issue

Describe the bug I am using cognito with serverless-http app. I encountered some error about Reference error in Cloudwatch when I request my serverless app.

ReferenceError: fetch is not defined
at Client.request (/var/task/node_modules/amazon-cognito-identity-js/lib/Client.js:55:5)
at /var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:313:21
at AuthenticationHelper.getLargeAValue (/var/task/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:85:7)
at CognitoUser.authenticateUserDefaultAuth (/var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:291:26)
at CognitoUser.authenticateUser (/var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:252:19)
at login (/var/task/src/serverless.js:277:17)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/task/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)

To Reproduce Steps to reproduce the behavior:

  1. Go to ‘…’
  2. Click on ‘…’
  3. Scroll down to ‘…’
  4. See error

Expected behavior

So I tried to fix this error to edit amazon_cognito_identity-js in node_modules. But it can not be applied, so I carefully change above these commit. https://github.com/Q00/amazon-cognito-identity-js/commit/6a907ddcbee687495bf9c3a2286480606a454b05

After change, finally I get intended response like this

message: 'User does not exist.',
code: 'UserNotFoundException',
time: 2019-01-20T19:31:41.735Z,
requestId: '0364989e-1cea-11e9-a193-affb587e0cbd',
statusCode: 400,
retryable: false,
retryDelay: 28.517921067981966 

I am junior with cognito so that I do not understand your intention perfectly. But these commit can help me deploy and service app without error. https://github.com/Q00/amazon-cognito-identity-js/commit/6a907ddcbee687495bf9c3a2286480606a454b05

Screenshots

image

Desktop (please complete the following information):

  • OS: mac os

Additional context #403 this issue can not help me so I tried to edit this repo directly. I want to help people who get related this error. What can I do for this error?

You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
elorzafecommented, Jan 21, 2019

I just tried this on node.js

global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');

var authenticationData = {
 Username: 'xxxxxxxx',
 Password: 'xxxxxxxx*',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
 UserPoolId: 'xx-xxxx-x_xxxxxxxxx', // Your user pool id here
 ClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx' // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
 Username: 'xxxxxxx',
 Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
 onSuccess: function (result) {
  var accessToken = result.getAccessToken().getJwtToken();
  console.log({ accessToken });
 },

 onFailure: function (err) {
  alert(err.message || JSON.stringify(err));
 },

});

I installed node-fetch on my node.js app

2reactions
lfadescommented, Jul 18, 2019

@chaitanya11 For Typescript you can do this:

(global as any).fetch = require('node-fetch');
Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: fetch is not defined - javascript - Stack Overflow
js, the fetch API is not installed by default. The best thing for you to do is to check whether the libraries are...
Read more >
ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >
How to fix 'ReferenceError: fetch is not defined' in Node.js
First, let's replicate the issue. You can update the index.js to the following and run node index.js , you should be able to...
Read more >
How do I fix ReferenceError: fetch is not defined - Replit
const fetch = (...args) => import('node-fetch').then ...
Read more >
ReferenceError: fetch is not defined in Node.js - DebugPointer
In conclusion, the ReferenceError occurs if you are using an older version ( < 18) of Node.js and it can be resolved by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found