const worker = new Worker('./counter.js')
my index.ts file
import { Worker, isMainThread } from "worker_threads";
if (isMainThread) {
const worker = new Worker('./counter.js')
worker.on('message', (data) => {
console.log(data)
})
worker.on('error', (err) => {
console.log(err)
})
}
my counter.ts file
`import { parentPort } from "worker_threads";
let count = 0;
for(let i=0;i<2000;i++){
count++
}
parentPort.postMessage(count)
my package.json file
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"node dist/index.js",
"build": "tsc",
"dev":"ts-node src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
my tsconfig.json file
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2020",
"sourceMap": true,
"outDir": "dist",
},
"include": ["src/**/*"],
}
require('./counter')
when i tested with ejs and commonjs it works fine but when passing the file name inside Worker()
it shows the error of module not foundyour text