Closed MichaelLiss closed 4 years ago
Auth.js is in the same directory as the file that spawns the worker but
full path fails:
let workerScript = path.join(__dirname, '/auth.js');
(node:13840) UnhandledPromiseRejectionWarning: Error: Cannot find module 'c:\Users\mliss\Documents\GitHub\instanttiming\routes\api\c:\Users\mliss\Documents\GitHub\instanttiming\routes\api\auth.js'
and relative path:
workerScript = './auth.js';
// or
workerScript = './auth';
gives this error and then just disappears
SyntaxError
Hey @MichaelLiss!
Here is the entire code
Does not look like the entire code. Where do you import from threads
?
My best guess is that you use require()
in the master thread code and have the ES imports in the worker code that node.js chokes upon.
Btw, are you transpiling the code in any way – Babel, webpack, …?
Thanks for replying...
installed it like this : (as my node version is 12.16.0 )
npm install threads
here is the stuff I have modified in order to just get it to the point it will fail:
if I do this at the top of my file:
import { spawn, Thread, Worker } from "threads";
I get this:
Waiting for thed:\Code\Udemy\instanttiming\routes\api\garminFunctions.js:1
import { spawn, Thread, Worker } from "threads";
^^^^^^
SyntaxError: Cannot use import statement outside a module
So I changed it to:
const { spawn, Thread, Worker } = require("threads");
I changed my code to be JUST like the Quick start code (except the auth.js file is in the same directory as the file that calls it )
so my function looks like this:
startWorker_Threads = async () => {
const auth = await spawn(new Worker("./auth"))
const hashed = await auth.hashPassword("Super secret password", "1234")
console.log("Hashed password:", hashed)
await Thread.terminate(auth);
}
ok.. got it... you were correct changed auth.js to
// workers/auth.js - will be run in worker thread
//import sha256 from "js-sha256"
const { expose } = require("threads/worker");
expose({
hashPassword(password, salt) {
return 'faking it';
}
})
Hi
I am using Visual Code on a Windows box.
node version: 12.16.0 npm version: 6.14.4
package dependencies:
When I start the debugger and step through the code, my app just disappears on this line: (it doesn't hit the catch either)
Here is the entire code:
Auth.js contains the following: