geckosio / phaser-on-nodejs

Allows you to run Phaser 3 game (including Phaser's physics engines) on Node.js
MIT License
103 stars 11 forks source link

Relative assets paths resolve to the `node_modules` folder #15

Open markjlorenz opened 2 years ago

markjlorenz commented 2 years ago

I'm getting the following error

[ERROR] 19:16:15 Error: ENOENT: no such file or directory, open '/app/node_modules/@geckos.io/phaser-on-nodejs/assets/tiles/map.json'
Error: ENOENT: no such file or directory, open '/app/node_modules/@geckos.io/phaser-on-nodejs/assets/tiles/map.json'

My code looks like this:

// src/scenes/Preloader.js
import Phaser from 'phaser';

export default class Preloader extends Phaser.Scene {
    constructor() {
        super("preloader")
    }

    preload() {
        this.load.tilemapTiledJSON("map", "../assets/tiles/map.json")
    }

    create() {
        this.scene.start("game")
    }
}

The full (correct) file path for the the asset is:

/app/src/assets/tiles/map.json

I tried calling this.load.setPath() at the top of Preloader.js#preload, thinking that I might just be able to reset the relative path, but that didn't have any effect.

I see the test for this behavior, but I wonder if it's passing because it also runs in the @geckos.io file path.

I'm using Phaser 3.52.0 `

markjlorenz commented 2 years ago

I found a workaround using __dirname:

import path from "path"

this.load.tilemapTiledJSON("map", path.join(__dirname, "../assets/tiles/map.json"))
FedeJure commented 2 years ago

Im facing the same issue but In my case is not that simple. I need to load an asset pack, but due I cannot use the setUrl or setPath is not working :S. there is any chance to have this solved? ty!

zewa666 commented 2 years ago

try setting this.load.setBaseUrl instead. Here's an example that redirects it for use with unit tests https://github.com/zewa666/phaser-vitest-bug/blob/master/src/scenes/Game.ts#L9