NodeJS MODULE_TYPELESS_PACKAGE_JSON error after nodejs update

NodeJS MODULE_TYPELESS_PACKAGE_JSON error after nodejs update

de Aleksi Postari -
Número de respuestas: 2

Hi!


I have updated the built-in NodeJS version that came with Coderunner to latest NodeJS version 22.7.0. I did this update manually in the Jobe server. This allowed us to use all the latest features, like string.replaceAll().


Now, the issue I am seeing is below. I have attached the answer, test case, and output below, where the output below throws this ugly MODULE_TYPELESS_PACKAGE_JSON error:


Question Type:

NodeJS


Answer:

    const simulatedServer = async (simulatedError) => { //tänne ei tarvitse koskea

        // Simuloi palvelinviivettä

        const delay = 500; //viive 3s

        await new Promise(resolve => setTimeout(resolve, delay));

        return simulatedError ? {response:"error", delay:delay} : {response:"ok", delay:delay,message:"Hey there!"};

    }


    const theFunction = async (simulatedError) => {

        // Kirjoita koodisi tähän

        console.log("odota");

        const result = await simulatedServer(simulatedError);


        if(result.response === "error"){

            return "Server Error";

        }else{

            return result.message ?? "Default message";

        }

    };


Test Case 1:

let result = await theFunction();

console.log(result)

result = await theFunction(true);

console.log(result)


Output:

odota
Hey there!
odota
Server Error

***Run error***
(node:281667) [MODULE_TYPELESS_PACKAGE_JSON] Warning: file://__tester__.js parsed as an ES module because module syntax was detected; to avoid the performance penalty of syntax detection, add "type": "module" to /package.json
(Use `nodejs --trace-warnings ...` to show where the warning was created)

Now, I have been figuring that I might need to just add the --input-type=module to the nodejs command that gets run, or add package.json file to the folder where the code is executed where I add "type": "module", but I am not sure which is the location, or how to edit that nodejs executable in Jobe to include that extra parameter. Any ideas? :)

En respuesta a Aleksi Postari

Re: NodeJS MODULE_TYPELESS_PACKAGE_JSON error after nodejs update

de Aleksi Postari -
Update: I think I just fixed this. After updating NodeJS to the latest version, I opened my jobe server and edited the file /var/www/html/jobe/application/libraries/nodejs_task.php. First, I tried adding just the --input-type=module to this line array parameters: $this->default_params['interpreterargs'] = array('--use_strict'); but it caused another new ugly error:

***Run error***
node:internal/modules/esm/resolve:1032
if (inputTypeFlag) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
^

Error [ERR_INPUT_TYPE_NOT_ALLOWED]: --input-type can only be used with string input via --eval, --print, or STDIN
at defaultResolve (node:internal/modules/esm/resolve:1032:32)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:554:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:523:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
at onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:481:36)
at TracingChannel.tracePromise (node:diagnostics_channel:337:14)
at ModuleLoader.import (node:internal/modules/esm/loader:480:21)
at node:internal/modules/cjs/loader:1373:28
at asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:11)
at Object.runEntryPointWithESMLoader (node:internal/modules/run_main:139:19) {
code: 'ERR_INPUT_TYPE_NOT_ALLOWED'

Now, I removed that --input-type=module parameter from the array and changed the executable javascript file to be saved as .mjs (module javascript) instead of .js extension by editing these two functions (compile and getTargetFile):


Original functions:

public function compile() {
$this->executableFileName = $this->sourceFileName;
if (strpos('.js', $this->executableFileName) != strlen($this->executableFileName) - 3) {
$this->executableFileName .= '.js';
}
if (!copy($this->sourceFileName, $this->executableFileName)) {
throw new exception("Node_Task: couldn't copy source file");
}
}

public function getTargetFile() {
return $this->sourceFileName;
}


And edited functions to make the runnable script be saved as .mjs:

public function compile() {
$this->executableFileName = preg_replace('/\.js$/', '.mjs', $this->sourceFileName);
if (!copy($this->sourceFileName, $this->executableFileName)) {
throw new exception("Node_Task: couldn't copy source file");
}
}

public function getTargetFile() {
return preg_replace('/\.js$/', '.mjs', $this->sourceFileName);
}


Right now, it seems like it's all working :)
En respuesta a Aleksi Postari

Re: NodeJS MODULE_TYPELESS_PACKAGE_JSON error after nodejs update

de Richard Lobb -
Thanks for posting, Aleksi. It looks like we'll have to make these changes - or something similar - for the next release.