Error: spawn node ENOENT - javascript

I'm working on my Electron app with Express server and when I build it with electron-packager, I get an error.
Uncaught Exception:
Error: spawn node ENOENT
at exports._errnoException (util.js:1024:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:192:19)
at onErrorNT (internal/child_process.js:374:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:607:11)
at startup (bootstrap_node.js:167:16)
at bootstrap_node.js:589:3
here is my main.js where child process is called
const cp = require('child_process');
let instance = cp.spawn('node',['./app.js']);
var electron = require('electron');
var browserWindow = electron.BrowserWindow;
var app = electron.app;
app.on('ready', function(){
// appWindow
var appWindow;
appWindow = new browserWindow({
width:1120,
height:620,
webPreferences: {
plugins: true
},
icon: __dirname + '/public/icon/icon.png'
});
appWindow.loadURL('file://' +__dirname + '/public/prva.html');
//appWindow.webContents.openDevTools();
});
// close app after all windows are closed
app.on('window-all-closed', () => {
app.quit()
})
Does anyone know solution for this error?

You most likely have a problem with your app.js script. You should hook onto some of the event listeners to understand what's really going on. There's an error, exit, close, disconnect, and message events you can listen for. You can also hook onto a number of other things such as the stdin, stdout, stderr as well. Checkout the documentation for the different events, hook onto all of them and output some information and you should be able to track down the issue.
You should also check to see if your express services runs properly directly from the command line rather than from within this Electron app. If it does then you've likely got a path wrong. This could be either that child_process cannot find the "node" application to run, or it can't find your startup script. In either case, you can use the path module to build up the correct path.

Related

how to run puppeteer application in replit?

i want to run whatsapp-web.js module in replit using node.js
I tried the program in the library documentation. namely as follows
const qrcode = require('qrcode-terminal');
const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth()
});
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async msg => {
const text = msg.body.toLowerCase() || '';
//check status
if (text === '!ping') {
msg.reply('pong');
}
});
client.initialize();
but when run. i get an error like this
/home/runner/coba-puppeteer/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
reject(new Error([
^
Error: Failed to launch the browser process!
/home/runner/coba-puppeteer/node_modules/whatsapp-web.js/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/home/runner/coba-puppeteer/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
at Interface.<anonymous> (/home/runner/coba-puppeteer/node_modules/whatsapp-web.js/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
at Interface.emit (node:events:525:35)
at Interface.emit (node:domain:489:12)
at Interface.close (node:readline:590:8)
at Socket.onend (node:readline:280:10)
at Socket.emit (node:events:525:35)
at Socket.emit (node:domain:489:12)
at endReadableNT (node:internal/streams/readable:1358:12)
repl process died unexpectedly: exit status 1
I've found the problem I may be having is that it doesn't have the libraries required by puppeteer on the system. For example, the error message says "libgobject-2.0.so.0" could not be found. It may be a library that puppeteer needs to run. try installing the required libraries by running the following command in your terminal: sudo apt-get install libgobject-2.0-0
however replit cannot use sudo command
with error sudo: The "no new privileges" flag is set, which prevents sudo from running as root.
has anyone ever managed to run a program using whatsapp-web.js in replit?
i am very confused what should i do
the expected output is that it will print the qrcode from whatsapp on the terminal

child_process.exec tries to launch node (instead of shell command) in some odd circumstances

I am trying to run some simple commands using child_process.exec. But in some odd corner cases, it tries to execute the given command with node, rather than the given (or default) shell.
Results
E.g., given below code, I get the following results:
cmd
Result
❌
'"yarn" -v'
Cannot find module 'C:\cwd\yarn.js'
❌
'"npm" -v'
Cannot find module 'C:\cwd\node_modules\npm\bin\npm-cli.js'
✅
'yarn -v'
(works as expected)
✅
'"C:\path\to\yarn" -v'
(works as expected)
OS: Win10
Node: v16., v17. (tried several of them)
EDIT: This seems to be a cmd-specific problem. If I set processOptions.shell = 'bash', it does not occur.
Full Error Message
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'C:\cwd\yarn.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Done - code 1 signal null
Thoughts
It appears that there is a special logic that is triggered, iff the "executable" is in double quotes, and has no spaces or path separators in them...?
Not sure if feature or bug?
Code
// test.js
const cp = require('child_process');
const cmd = '"yarn" install';
const child = cp.exec(cmd);
// ###########################################################################
// process monitoring
// ###########################################################################
child.on('exit', (code, signal) => {
console.log('Done - code', code, ' signal', signal);
});
child.on('error', (err) => {
console.error(`Error:`, err);
});
// inherit stdio
child.stdout.pipe(process.stdout);
process.stdin.pipe(child.stdin);
child.stderr.pipe(process.stderr);
Turns out, it is a volta bug. I filed it here.
Easy to verify: Change the command to do other things on top of "yarn" install. The following still shows hi, but the yarn part still bugs out, so it is definitely not an issue with node or cmd:
const cmd = 'echo "hi" && "yarn" install';
const child = cp.exec(cmd);
Some more explanation: volta manages node, npm and yarn for you, making it very easy to install/uninstall/pin versions globally or even per folder/project. That means that "yarn" install does not actually run yarn, but actually this:
Runs a volta executable
Which will then run yarn...
...which evidently messed up the logic to look up the actual executable.
This happens only with yarn and npm. Everything else (thus far) seems fine, including node itself.

SocketCluster Client -- TypeError: WebSocket is not a constructor

I am new to javascript and am trying to develop a react.js application including communication over socketcluster framework. The client should not run in the browser but in a separate javascript file in the background.
To realize this, I have installed the necessary modules for server (https://github.com/SocketCluster/socketcluster) and client (https://github.com/SocketCluster/socketcluster-client).
I followed the instructions and the communication between server and client (javascript code embedded in html, run in browser) worked. But when I try to run the client in a seperate javascript (gbab-client.js) file with "node gbam-client.js", it doesn't.
I would be very grateful for your help!
Content of gbab-client.js:
const socketClusterClient = require('socketcluster-client/socketcluster');
const options = {
port: 2222
};
// Initiate the connection to the server
const socket = socketClusterClient.connect(options);
console.log('Connecting...');
socket.on('connect', function () {
console.log('CONNECTED');
});
// Listen to an event called 'rand' from the server
socket.on('rand', function (num) {
console.log('RANDOM: ' + num);
});
Error Message:
$ node gbam-client.js
C:\...\node_modules\socketcluster-client\socketcluster.js:1306
return new WebSocket(uri, null, options);
^
TypeError: WebSocket is not a constructor
at createWebSocket (C:\...\node_modules\socketcluster-client\socketcluster.js:1306:12)
at new SCTransport (C:\...\node_modules\socketcluster-client\socketcluster.js:1337:18)
at SCClientSocket.connect.SCClientSocket.open (C:\...\node_modules\socketcluster-client\socketcluster.js:558:22)
at new SCClientSocket (C:\...\node_modules\socketcluster-client\socketcluster.js:433:10)
at Object.create (C:\...\node_modules\socketcluster-client\socketcluster.js:198:31)
at Object.module.exports.create (C:\...\node_modules\socketcluster-client\socketcluster.js: 14:18)
at Object.<anonymous> (C:\...\gbam-client.js:12:36)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
UPDATE:
I found the solution. The destination of require wasn't right.
This is the corrected code (gbam-client.js):
const socketClusterClient = require('socketcluster-client');
const options = {
hostname:'localhost',
port: 2222
};
...
Maybe your ws (WebSocket library) version is not compatible with socketcluster, try to update it WS

spawn ls ENOENT when watched file is modified

I am learning Node.js via the book Node.js the Right Way. I am trying to run the following example to watch changes to a file called target.txt that resides in the the same directory as the .js file.
"use strict";
const
fs = require('fs'),
spawn = require('child_process').spawn,
filename = process.argv[2];
if (!filename) {
throw Error("A file to watch must be specified!");
}
fs.watch(filename, function () {
let ls = spawn('ls', ['-lh', filename]);
ls.stdout.pipe(process.stdout);
});
console.log("Now watching " + filename + " for changes...");
I get the following error when I change the text file and save it:
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn ls ENOENT
at exports._errnoException (util.js:1018:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Node.js version: v6.11.0
IDE: Visual Studio Code 1.13.1
OS: Windows 10 64x
There's no ls on Windows, you should use dir instead.
However, that's not an executable. To run .bat and .cmd files you can:
Spawn cmd.exe and pass those files as arguments:
require('child_process').spawn('cmd', ['/c', 'dir']);
Use spawn with the shell option set to true:
require('child_process').spawn('dir', [], { shell: true });
Use exec instead of spawn:
require('child_process').exec('dir', (err, stdout, stderr) => { ... });
For more on that, take a look at this section in the official docs.
EDIT:
I'm not sure I understood you question in the comment correctly, but if you go for the second option, for example, you code will look like this:
...
fs.watch(filename, function () {
let dir = spawn('dir', [filename], { shell: true });
dir.stdout.pipe(process.stdout);
});
...
Please, keep in mind you may need to adjust this code slightly. I'm writing all this from memory as I don't have access to a Windows machine right now, so I can't test it myself.

Can't connect Mongodb to Node.js, MEAN-stack

I'm a Mac user and I'm very new to MEAN-stack. I'm trying to connect a Mongoose with my Node.js but it always show this error
events.js:141
throw er; // Unhandled 'error' event
^
Error: failed to connect to [undefined:27017]
at null.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:556:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:996:8)
at doNTCallback2 (node.js:452:9)
at process._tickCallback (node.js:366:17)
I can use both Mongoose and Mongodb normally in my terminal.
Here is my server.js
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var mongoose = require('./config/mongoose'); //connect DB
var express = require('./config/express');
var db = mongoose();
var app = express();
app.listen(3000);
module.exports = function(){
require('../app/routes/index.routes')(app);
require('../app/routes/user.routes')(app);
};
module.exports = app;
And it calls mongoose.js
var config = require('./config');
var mongoose = require('mongoose');
console.log('Mongoose.js');
console.log(config);
module.exports = function(){
mongoose.set('debug', config.debug);
var db = mongoose.connect(config.monoUri);
console.log('Call User Model');
require('../app/models/user.model');
return db;
};
Then calls config.js and development.js
/////config.js/////
module.exports = require('./env/' + process.env.NODE_ENV + '.js');
/////development.js/////
module.exports = {
debug: true,
mongoUri: 'mongodb://localhost/webrtc',
sessionSecret: 'dev_secret_key'
};
Please help. I have been trying to debug this about 2 days and I got nothing new. Anything miss?
The error is pointing you towards the issue:
Error: failed to connect to [undefined:27017]
So there's something wrong with the location of the database, it looks like the hostname is undefined.
Here's your connection code:
var db = mongoose.connect(config.monoUri);
And here your configuration:
mongoUri: 'mongodb://localhost/webrtc'
Can you spot the typo in your connection code? It says monoUri, not mongoUri.
can you do
npm install mongoose
and also post the output from
ps -ef | grep mongod
All on the the machine you are running node from. That would go a long way towards the rest of an answer.
Also follow the simple sample code- https://github.com/mongodb/node-mongodb-native#introduction, substitute the database name and a collection name and run that on node
node sample.js

Categories