j2inn / haystack-nclient

Project Haystack client network TypeScript library
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

fetch is not defined #20

Closed federicogatti closed 2 years ago

federicogatti commented 2 years ago

Trying to use the client I recevice this error

ReferenceError: fetch is not defined
    at requestFinAttestKey (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:328:18)
    at getAttestKey (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:348:19)
    at addAttestKeyHeader (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:387:33)
    at finCsrfFetch (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:400:15)
    at fetchVal (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:654:26)
    at OpsService.invokeOp (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:3162:16)
    at OpsService.read (/home/federico/evogy/haystack/auth/node_modules/haystack-nclient/dist/index.js:3178:25)
    at Object.<anonymous> (/home/federico/evogy/haystack/auth/index.js:17:25)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

Code is very simple


var { Client } = require('haystack-nclient')

// Explicitly define what project to use...
let client = new Client({
    base: new URL("url"),
    project: 'demo',
    authBearer: '***'
});

const grid = client.ops.read('site')
    .then(value => console.log(value))
    .catch(error => console.log(error));

package.json

{
  "name": "auth-haystack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@skyfoundry/haystack-auth": "^1.0.0",
    "haystack-core": "^2.0.34",
    "haystack-nclient": "^3.0.23",
    "haystack-units": "^1.0.17",
    "node-fetch": "^2.6.1"
  }
}

npm version 8.5.1 node version 14.19.0

federicogatti commented 2 years ago

I solved it installing node-fetch with npm and importing it globally

import fetch from 'node-fetch'

global.fetch=fetch