ReferenceError: fetch is not defined

I'm trying to use aws-sdk and aws-cognito-identity-js library with node-red-contrib-function-npm node. I have some questions.

I wrote some code and executed but occurred ReferenceError: fetch is not defined
Do you have any suggestion?

var fetch = require("node-fetch");
global.set("fetch",fetch);

var AWS = require("aws-sdk");
var AmazonCognitoIdentity  = require("amazon-cognito-identity-js");

var email = "email";
var passwd = "password";

var poolData={
    "UserPoolId": 'userpoolid',
	"ClientId": 'clientid'
}

var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

var authData = {
	Username : email,
	Password : passwd
};
var authDetail = new AmazonCognitoIdentity.AuthenticationDetails(authData);

var cognitoUser = new AmazonCognitoIdentity.CognitoUser(activateInfo);

cognitoUser.authenticateUser(authDetail, {
		onSuccess : function(result) {
			accessToken = result.getIdToken().getJwtToken();          
            console.log("idToken:"+accessToken);
		},
		onFailure : function(err) {
			console.log('error: ' + err);
	});

Did you add the necessary require to your settings.js file global context?

Thanks to reply my topic!
I didnt edit my setting.js file.
Do I need to edit it even if i use node-red-contrib-function-npm node?

I added fetch:require("cross-fetch/polyfill"), to setting.js
and const fetch = global.get("fetch"); to function-npm node.

I finally got idtoken.

Thanks,dceejay!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.