Having postMessage is not a function error when using it on nodejs.
invasionofsmallcubes opened this issue ยท 7 comments
Hi,
as per readme, I installed this library with npm i stockfish
.
Whenever I try to run it
const stockfish = require('stockfish')
const engine = stockfish()
engine.onmessage = function (message) {
console.log("received: " + message);
}
engine.postMessage('setoption name Contempt value 30');
engine.postMessage('setoption name Skill Level value 20');
engine.postMessage('ucinewgame');
engine.postMessage('isready');
I get
00:33:31 web.1 | engine.postMessage('setoption name Contempt value 30');
00:33:31 web.1 | ^
00:33:31 web.1 | TypeError: engine.postMessage is not a function
I start node with --experimental-wasm-modules
enabled.
Tried many things with version 12 nothing made it works. Downgraded to version 11, sample code works on first try so it has to do with the update.
Is there any update on this? It still gives postMessage is not a function
error above version 11.
Hello!
This error is happening because when you call
const stockfish = require('stockfish')
const engine = stockfish()
the engine
is actually an asyncronous function (it's not the engine itself yet)
You can check it by calling console.log(engine)
, and you'll see that it prints [Function (anonymous)]
to the console.
The correct usage would be calling this engine
function, as you can imagine, it will return a promise to the real engine, so you can use a .then()
to atribute the value of this promise to any variable, and finally, call .postMessage
through this variable.
Example:
const stockfish = require('stockfish')
const engine = stockfish();
let myEngine;
console.log(engine)
async function run_engine(){
await engine().then((sf) => {
myEngine = sf;
myEngine.onmessage = function (message) {
console.log("received: " + message);
}
});
myEngine.postMessage('setoption name Contempt value 30');
myEngine.postMessage('setoption name Skill Level value 20');
myEngine.postMessage('ucinewgame');
myEngine.postMessage('isready');
}
run_engine();
The code on this example shall work as you were expecting
Thanks for the help! I tried and .postMessage is not a function
error is gone, but I'm getting pthread sent an error! undefined:undefined: Cannot find module '....\node_modules\stockfish\src\stockfish.worker.js'
now. Do you have any idea?
@nihatemreyuksel Yeah...I got stuck on this one as well. But looking at the examples I found another approach that worked for me, and it's basically the node_abstraction example
I coppied the load_engine.js file inside my project folder, than I just used the code:
#!/usr/bin/env node
var prompt = require("prompt-sync")({ sigint: true });;
var loadEngine = require("./load_engine.js");
// you need to install stockfish via npm
var engine = loadEngine(require("path").join(__dirname, "node_modules/stockfish/src/stockfish.js"));
cmd = prompt("cmd: ");
engine.send(cmd, function onDone(data){
console.log("DONE:", data);
},
function onStream(data){
console.log("STREAMING:", data);
});
And it worked, so now everytime I want to send a command to stockfish I use engine.send()
and deal with the responses on the onStream
parameter
Check it out to see if it works
Getting the exact same problem as @invasionofsmallcubes and after finding out the engine is async getting the exact problem as @nihatemreyuksel
Thank you for the help @fatorius but
1 - it looks like a not pretty at all workaround, I believe the issue should still be fixed
2 - your code seems incomplete, nothing calls the onStream
method
Tried many things with version 12 nothing made it works. Downgraded to version 11, sample code works on first try so it has to do with the update.
This works for me.
const stockfish = require('stockfish');
const engine = await stockfish();