storyblok / storyblok-js-client

Universal JavaScript client for Storyblok's API
MIT License
129 stars 87 forks source link

ReferenceError: Headers is not defined #591

Closed romainl closed 1 year ago

romainl commented 1 year ago

Using the management API to create a component, the error below is thrown and the operation fails.

ReferenceError: Headers is not defined

Expected Behavior

The given component is created in the given space and I get a representation of the component as response.

Current Behavior

The following error is thrown and the operation fails.

ReferenceError: Headers is not defined
    at new $ (/path/to/project/node_modules/storyblok-js-client/dist/index.umd.js:24:6888)
    at Object.<anonymous> (/path/to/project/component.js:3:19)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47

Steps to Reproduce

  1. Create a new project directory: $ mkdir sb-test.

  2. Change to that directory: $ cd sb-test.

  3. Initialize the node project: $ npm init -y.

  4. Install storyblok-js-client: $ npm i storyblok-js-client`.

  5. Create component.js with the following content (use your own auth token and space id):

    const StoryblokClient = require('storyblok-js-client');
    
    const Storyblok = new StoryblokClient({
     oauthToken: 'XXXXX'
    })
    
    const spaceId = 'YYYYY'
    
    Storyblok.post(`spaces/${spaceId}/components/`, {
     "component": {
       "name": "teaser",
       "display_name": "Teaser",
       "schema": {
         "title": {
           "type": "text",
           "pos": 0
         },
         "image": {
           "type": "image",
           "pos": 1
         }
       },
       "is_root": false,
       "is_nestable": true
     }
    }).then(response => {
     console.log(response)
    }).catch(error => { 
     console.log(error)
    })
  6. Run the code: $ node component.js.

A few notes:

Environment

romainl commented 1 year ago

Upgrading Node to 18+ fixed the issue.