# Phaser headless not working

**URL:** <https://phaser.discourse.group/t/phaser-headless-not-working/5385>\
**Category:** Phaser 3\
**Created:** [March 3, 2020, 3:45pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385 "2020-03-03T15:45:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdebojyoti](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gdebojyoti/32/2660_2.png) [@gdebojyoti](https://phaser.discourse.group/u/gdebojyoti)\
**Post date:** [March 3, 2020, 3:45pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/1 "2020-03-03T15:45:01Z")

</div>

I have the following code (simplified):

```
import Phaser from 'phaser'

const config = {
  type: Phaser.HEADLESS,
  width: 1024,
  height: 640,
  physics: {
    default: 'arcade',
    arcade: {
      gravity: {
        y: 980
      },
      debug: true
    }
  },
  scene: {
    preload: () => { console.log('server preload') },
    create: () => { console.log('server create') },
    update: () => { console.log('server update') }
  },
  title: 'Phaser server app',
  backgroundColor: '#06C6F8',
  transparent: true,
  disableContextMenu: true
}

class Game {
  static initialize () {
    console.log('initializing server game');
    (() => new Phaser.Game(config))()
  }
}

export default Game

```

I trigger this by writing `Game.initialize()`.

In the node devtools console, the “initializing server game” text shows up.  
So does “Phaser server app / Phaser v3.22.0-FB (Headless | HTML5 Audio)”.

However, the actual app itself is not running since the preload, create & update methods do not seem to work (their respective outputs are not visible in the console).

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [March 3, 2020, 6:16pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/2 "2020-03-03T18:16:10Z")

</div>

I just tested it in node v10.19.0.  
The setup below works 🙂

```javascript
require('@geckos.io/phaser-on-nodejs')
const Phaser = require('phaser')

const config = {
  type: Phaser.HEADLESS,
  width: 1024,
  height: 640,
  physics: {
    default: 'arcade',
    arcade: {
      gravity: {
        y: 980
      },
      debug: true
    }
  },
  // disable audio
  audio: {
    noAudio: true
  },
  scene: {
    preload: () => {
      console.log('server preload')
    },
    create: () => {
      console.log('server create')
    },
    update: () => {
      // console.log('server update')
    }
  },
  title: 'Phaser server app',
  backgroundColor: '#06C6F8',
  transparent: true,
  disableContextMenu: true
}

class Game {
  static initialize() {
    console.log('initializing server game')
    ;(() => new Phaser.Game(config))()
  }
}

Game.initialize()

```

---

<div class="post-metadata">

**Author:** ![gdebojyoti](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gdebojyoti/32/2660_2.png) [@gdebojyoti](https://phaser.discourse.group/u/gdebojyoti)\
**Post date:** [March 3, 2020, 8:50pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/3 "2020-03-03T20:50:31Z")

</div>

I am doing the same thing. 😭

Is something wrong with my implementation of JSDOM?

**server.js**

```
require('@babel/register')({
  presets: ['@babel/preset-env']
})

const { JSDOM } = require('jsdom')
require('canvas')

const jsdom = new JSDOM('<!doctype html><html><body></body></html>')
const { window } = jsdom

function copyProps (src, target) {
  Object.defineProperties(target, {
    ...Object.getOwnPropertyDescriptors(src),
    ...Object.getOwnPropertyDescriptors(target)
  })
}

global.window = window
global.document = window.document
global.navigator = {
  userAgent: 'node.js'
}
global.requestAnimationFrame = function (callback) {
  return setTimeout(callback, 0)
}
global.cancelAnimationFrame = function (id) {
  clearTimeout(id)
}
copyProps(window, global)

module.exports = require('app.js')

```

**app.js** -

```
import express from 'express'
import Game from 'Game.js'

// code... use express

Game.initialize() // start phaser app

```

Most of the original code (i.e., in the question) was part of **Game.js**.

Lastly, I start the server using `node server.js`.

---

<div class="post-metadata">

**Author:** ![gdebojyoti](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gdebojyoti/32/2660_2.png) [@gdebojyoti](https://phaser.discourse.group/u/gdebojyoti)\
**Post date:** [March 3, 2020, 8:56pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/4 "2020-03-03T20:56:38Z")

</div>

My Github repo (with actual code) -

> **[gdebojyoti/lightfury](https://github.com/gdebojyoti/lightfury/tree/forum_question)**
>
> Contribute to gdebojyoti/lightfury development by creating an account on GitHub.

**server.js (starting point)**

> <https://github.com/gdebojyoti/lightfury/blob/forum_question/scripts/server.js>

**app.js**

> <https://github.com/gdebojyoti/lightfury/blob/forum_question/src/server/app.js>

**Game.js**

> <https://github.com/gdebojyoti/lightfury/blob/forum_question/src/server/components/Game.js>

`node server.js`

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [March 4, 2020, 4:11pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/6 "2020-03-04T16:11:33Z")

</div>

I installed your forum\_question branch.

I do not know what you did wrong, but when I add `import '@geckos.io/phaser-on-nodejs'` on top of `Game.js` and run `npm run server:start`, it works.

---

<div class="post-metadata">

**Author:** ![gdebojyoti](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gdebojyoti/32/2660_2.png) [@gdebojyoti](https://phaser.discourse.group/u/gdebojyoti)\
**Post date:** [March 5, 2020, 2:25pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/7 "2020-03-05T14:25:11Z")

</div>

Thank you so much for going through the trouble. I really appreciate it. 🙂 🙂

Your Github repo solved my problem!

I went through your code here - [https://github.com/geckosio/phaser-on-nodejs/blob/master/src/index.ts](https://github.com/geckosio/phaser-on-nodejs/blob/master/src/index.ts)

I found the line that I had missed - `global.Image = Canvas.Image`.

Now my preload, create & update methods are being called as expected.

Once again, thank you so much!! 🙂 🙂 🙂

P.S. I am not sure why the lack of `Image` was preventing the 3 methods from being called.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 5, 2020, 5:03pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/8 "2020-03-05T17:03:36Z")

</div>

Phaser uses `new Image()` to load the default textures, and it can’t finish booting until they load.

---

<div class="post-metadata">

**Author:** ![gdebojyoti](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gdebojyoti/32/2660_2.png) [@gdebojyoti](https://phaser.discourse.group/u/gdebojyoti)\
**Post date:** [March 7, 2020, 8:28pm UTC](https://phaser.discourse.group/t/phaser-headless-not-working/5385/9 "2020-03-07T20:28:03Z")

</div>

I did think something similar might be happening; however, I don’t understand why no error showed up in the console.
