I am trying to call a function from an external file using child_process, but, it is returning to me an unexpected message and doesn’t even run the target function.
index.ts
import { fork } from 'child_process';
var cards = fork(`${__dirname}/workers/shuffleCards.js`, []);
cards.on("message", msg => { console.log(`Received message`, msg) });
cards.on("error", msg => { console.log(`Received error`, msg) });
cards.send("test");
shuffleCards.js
import cardShuffler from "../components/cardShuffler";
function shuffleCards() {
const pid = process.pid;
console.log(`Received process: ${pid}`);
var action_cards = cardShuffler.shuffleActions();
var property_cards = cardShuffler.shuffleProperties();
var money_cards = cardShuffler.shuffleMoney();
var rent_cards = cardShuffler.shuffleRent();
var wild_cards = cardShuffler.shuffleWilds();
var parsed_cards = [];
[action_cards, property_cards, money_cards, rent_cards, wild_cards].map(card => {
card.forEach(sub_card => {
parsed_cards.push(sub_card)
});
});
console.log(parsed_cards)
return parsed_cards;
}
process.once("message", shuffleCards);
Message on console.log
Received message {
compile: 'C:\Users\USER\source\repos\Will\src\components\cardShuffler.ts',
compiledPath: 'C:\Users\USER\AppData\Local\Temp\.ts-nodeeyTUFf\compiled\src_components_cardShuffler_ts_7ae359b0420e1268f12c87be4bf028cb159f9cb115715422295351cb6494e318.js'
}
So it is returning me this compile and compiledPath message and it does not execute the shuffleCards()
function