A
A
Alexander Mikhailov2016-01-12 13:05:22
Node.js
Alexander Mikhailov, 2016-01-12 13:05:22

"TypeError: fetch is not a function". How to be?

NODE.JS v5.4.0
Installed openpgp module:
npm install --save openpgp
There is this code:

var openpgp = require('openpgp');
var hkp = new openpgp.HKP('https://pgp.mit.edu');
hkp.lookup({
    query: '[email protected]'
}).then(function(key) {
    var publicKey = openpgp.key.readArmored(key);
});

I am getting an error:
return fetch(uri).then(function(response) {
TypeError: fetch is not a function
    at HKP.lookup (*\node_modules\openpgp\src\hkp\hkp.js:63:10)

fetch is not a function - how to overcome? Google doesn't save me...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Mikhailov, 2016-01-12
@chelaxe

Decision. Thanks to Alexey Ukolov for drawing my attention to the missing paragraph.

var fetch = typeof window !== 'undefined' ? window.fetch : require('node-fetch');
var hkp = new openpgp.HKP('https://pgp.mit.edu', fetch);

Well, we put "node-fetch"
npm install --save node-fetch

A
Alexey Ukolov, 2016-01-12
@alexey-m-ukolov

For the OpenPGP HTTP Key Server (HKP) client the new fetch api is used. There is a polyfill for both browsers and node.js runtimes. These are not bundled in the library however and users must add these themselves. See the unit tests for examples of how to integrate them.
https://github.com/openpgpjs/openpgpjs#dependencies

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question