How to bundle worker_thread dependencies with webpack - node.js

I'm writing an electron application that uses electron forge for setup and configuration.
The project uses Webpack 5 as a bundler and typescript as the development language. I'm having trouble running a worker_thread on the main-thread.
It all works fine, as long as the worker_thread does not use any imported local modules. But when I import modules they can't be found. I'm thinking this is because webpack doesn't bundle the dependencies from the worker.
My code looks like this:
index.ts (electron main thread):
ipcMain.handle('export', async (event, measurements: Measurement[], options: ExportOptions) => {
return new Promise((resolve, reject) => {
const worker = new Worker(new URL('./export.worker.js', import.meta.url))
worker.on('message', resolve)
worker.on('error', reject)
worker.postMessage({
measurements,
options,
})
})
})
export.worker.js (same folder as main.ts):
const { parentPort } = require('worker_threads')
const { Export } = require('../preload/export')
parentPort.on('message', async (data) => {
const { measurements, options } = data
await Export.export(measurements, options)
parentPort.close()
})
When I try to run this I get:
Error occurred in handler for 'export': Error: Cannot find module '../preload/export'
Require stack:
- C:\Development\spectro-demo\internal-client\.webpack\main\a3142abb2db60e4fab06.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
at Function.Module._load (internal/modules/cjs/loader.js:737:27)
at Module.require (internal/modules/cjs/loader.js:964:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Development\spectro-demo\internal-client\.webpack\main\a3142abb2db60e4fab06.js:2:20)
at Module._compile (internal/modules/cjs/loader.js:1083:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1113:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Development\\spectro-demo\\internal-client\\.webpack\\main\\a3142abb2db60e4fab06.js'
]
}
So I'm wondering: How can I use worker_threads that reference modules in my project?

I was able to fix it. Seems to be a bug in webpack 5. See this repo for a workaround: https://github.com/DustinJSilk/web-worker-ts-webpack-test

Related

Vercel Serverless function results in "Cannot find module 'content-type'" when accessing req.body

I'm using NextJS with Vercel Serverless as described in this answer - https://stackoverflow.com/a/63659707/2826679 . My NodeJS function fails with an error message when I'm running it locally using vercel dev or when it's deployed to vercel servers.
My <PROJECT_ROOT>/api/report.ts looks like this and it fails at printing "3 req.body".
import type { VercelRequest, VercelResponse } from '#vercel/node';
export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method === 'POST') {
console.log("1")
console.log("2", req)
console.log("3", req.body)
const data = {
test: "test"
};
return res.status(200).setHeader('content-type', 'application/json').end(JSON.stringify(data));
} else {
return res.status(404);
}
}
Error:
Unhandled rejection: Error: Cannot find module 'content-type'
Require stack:
- /Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/piotr/.config/yarn/global/node_modules/#cspotcode/source-map-support/source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:885:27)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.909 (/Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js:262:33)
at __webpack_require__ (/Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js:328:42)
at parseBody (/Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js:19:45)
at IncomingMessage.get [as body] (/Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js:212:27)
at handler (/Users/piotr/astrology/astroperspective/api/report.ts:7:26) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/piotr/.config/yarn/global/node_modules/#vercel/node-bridge/helpers.js'
]
}
EDIT:
Now, I'm thinking that it has to do something with dependencies in package.json since my package.json is specifically for NextJS
Were you by any chance able to find out what was causing this? I'm running into the same error as well.

How do I Fix a Cannot find module '.functions/handleCommands.js' error?

Upon running my Bot to have it sign-on after installing a command and event handler I got the error
Error: Cannot find module '.functions/handleCommands.js'
Require stack:
C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js:14:9
at Object.<anonymous> (C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js:19:3)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) { code:
'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\levan\\Desktop\\Discordbot\\Bot Templates\\2 command handling\\src\\bot.js'
After looking thru the folder functions and the handleCommands.js, I Still cannot figure out what I'm missing or what I might on messed up. I thought that maybe my module.exports was in the wrong place but moving things around did not solve the issue either.
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const clientId = 'redacted'; // your bots token
const guildId = 'redacted'; // the server id the bot will be on
module.exports = (clinet) => {
clinet.handleCommands = async (commandFolders, path) =>{
clinet.commandArray = [];
for(folder of commandFolders){
const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`../commands/${folder}/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
clinet.commandArray.push(command.data.toJSON());
}
}
};
};
const rest = new REST({ version: '9' }).setToken(process.env.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: clinet.commandArray },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
As the error was MODULE_NOT_FOUND I figured that maybe I didn't run all the installs I need to make my bot work. After running thru my npm lists the error persisted.
NPM's Installed
"npm init"
"npm install discord.js"
"npm install dotenv"
"npm install discord.js dotenv"
"npm install #discordjs/rest discord-api-types #discordjs/builders"
My goal is to be the Bot back online and to figure out how to troubleshoot this sort of error in case it comes up again. Any help you can give will be greatly appreciated.
'.functions/handleCommands.js' should have been './functions/handleCommands.js' Caffeine is not a substitute for sleep

yargs.command is not working (error msg: yargs.command is not a function)

I am trying to create yargs command, when I run the app I get an error.
When I run the following:
node app.js add
on my node.js code:
const yargs = require('yargs')
yargs.command({
command:'add',
describe:'Adding command',
handler:function(){
console.log('Adding notes')
}
}).parse()
console.log('yargs.argv')
Error:
C:\node\notes-app\app.js:3
yargs.command({
^
**TypeError: yargs.command is not a function**
at Object.<anonymous> (C:\node\notes-app\app.js:3:7)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Yargs version:1.1.0
node version: v14.16.0
vs code version
1.55.2(user setup) OS:Window 10
Edit:
(I have already viewed this question but it didn't help because already added .parse())
Try following lines.
const yargs = require('yargs')
const {hideBin} = require('yargs/helpers')
yargs(hideBin(process.argv)).command({
command:'add',
describe:'Adding command',
handler:function(){
console.log('Adding notes')
}
}).parse()
console.log('yargs.argv')
Yargs 1.1.0 is a long time ago, 7 years infact. Back then they didn't have the .command() method.
https://www.npmjs.com/package/yargs/v/1.1.0
The easiest solution is just to update. You should be able to go to atleast v14 or v15 with node v14.16.
If you don't want to you will need to add your own small command parser:
const argv = require('yargs').argv;
if(argv._[0] === "add") {
// ...
}

TypeError: Cannot read property 'showMessageBox' of undefined electron in node js

Below code is just to display the dialog box using electron node module.
app.js
const { dialog } = require('electron')
const response = dialog.showMessageBox(null);
console.log(response);
Need help to understand why I am getting below error message:
const response = dialog.showMessageBox(null);
^
TypeError: Cannot read property 'showMessageBox' of undefined
at Object.<anonymous> (C:\Users\1217688\Desktop\WebApp\node-elect-test\app.js:2:25)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
at bootstrap_node.js:662:3
In renderer process, add .remote
const { dialog } = require('electron').remote
Try calling the dialog module when the app is ready:
app.on('ready', () => {
const { dialog } = require('electron')
dialog.showMessageBox(null);
})

Unable to require node-wit

I had been using node-wit v3.3.2
Today, I wanted to update and use the latest version.
But I'm unable to import node-wit. Not sure why.
I simply copied the code given in their documentation.
'use strict'
var MY_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const {Wit, log} = require('node-wit');
const client = new Wit({
accessToken: MY_TOKEN,
actions: {
send(request, response) {
return new Promise(function(resolve, reject) {
console.log(JSON.stringify(response));
return resolve();
});
},
myAction({sessionId, context, text, entities}) {
console.log(Session ${sessionId} received ${text});
console.log(The current context is ${JSON.stringify(context)});
console.log(Wit extracted ${JSON.stringify(entities)});
return Promise.resolve(context);
}
},
logger: new log.Logger(log.DEBUG) // optional
});
The terminal shows this:
const {Wit, log} = require('node-wit');
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
Could be the node version you are using. You'll need to use the flag --harmony_destructuring when using a lower version.
Taken from: https://github.com/wit-ai/node-wit
# Node.js <= 6.x.x, add the flag --harmony_destructuring
node --harmony_destructuring examples/basic.js <MY_TOKEN>
# Node.js >= v6.x.x
node examples/basic.js <MY_TOKEN>

Resources