fetch issue running pre-trained USE model in Node.js
See original GitHub issueHad an issue with fetch
when loading a pre-trained TensorFlow.js model in a Node.js app.
System information
- Mac OS Mojave - 10.14.1
- Node.js - 10.15.1
- relevant npm packages:
- @tensorflow-models/universal-sentence-encoder@1.0.1
- @tensorflow/tfjs@1.0.0
- @tensorflow/tfjs-node@1.0.1
- express@4.16.4
- node-fetch@2.3.0
Issue
I’m using the pre-trained USE model in a simple Express server endpoint—it basically just returns the text embedding for an input string.
The relevant source (which lives in a separate file called by the endpoint):
// use tensorflow js node backend
require('@tensorflow/tfjs-node');
const universalSentenceEncoder = require('@tensorflow-models/universal-sentence-encoder');
/**
* Encode a variable length string as a sentence embedding via the Universal Sentence Encoder.
*
* @param {*} text - the input string
*/
exports.embed = async function embed(text) {
const result = universalSentenceEncoder.load().then(model => model.embed(text));
return result;
};
This appears to be related to #489, but my issue remained.
Calling the universalSentenceEncoder.load()
function causes a ReferenceError: fetch is not defined
error.
This appears to be the offending line: https://github.com/tensorflow/tfjs-models/blob/master/universal-sentence-encoder/src/index.ts#L49
It seems odd to me that tfjs-node
requires the node-fetch
package and yet still required a separate package to fix this.
The workaround I am using is overriding the global fetch with a line like this at the top of the file:
global.fetch = require('node-fetch');
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
@alexellis yes good point - thanks for pointing this out! We’ll patch this soon. Meanwhile you can override global fetch as a bandaid, but hopefully not for too much longer 😃
It’d be ideal if all examples worked on Node as well as in the browser.