Cannot spawn Python from node.js - javascript

I am trying to spawn a python process from node.js as such:
const { spawn } = require('child_process');
const process = spawn('python', ['--version']);
But this throws an error:
$ node server2.js
node:events:346
throw er; // Unhandled 'error' event
^
Error: spawn python ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
at onErrorNT (node:internal/child_process:480:16)
at processTicksAndRejections (node:internal/process/task_queues:81:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
at onErrorNT (node:internal/child_process:480:16)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn python',
path: 'python',
spawnargs: [ '--version' ]
}
Why am I getting this error?

const process = spawn('python', ['--version']);
need to be changed to:
const process = spawn('python3', ['--version']);

Related

getaddrinfo ENOTFOUND api.telegram.org (Telegraf)

This is my code
const config = require('./config.json');
const {Telegraf} = require('telegraf');
// require('dotenv').config();
const bot = new Telegraf(config.BOT_TOKEN);
bot.use((ctx) => {
ctx.reply('Bot has started');
});
bot.launch()
every time I run this code it give me error and the error is
D:\Danish\Node testing\Node Bot Testing\node_modules\node-fetch\lib\index.js:1491
reject(new FetchError(request to ${request.url} failed, reason: ${err.message}, 'system', err));
^
FetchError: request to https://api.telegram.org/bot5121286363:[REDACTED]/getMe failed, reason: getaddrinfo ENOTFOUND api.telegram.org
at ClientRequest. (D:\Danish\Node testing\Node Bot Testing\node_modules\node-fetch\lib\index.js:1491:11)
at ClientRequest.emit (node:events:527:28)
at TLSSocket.socketErrorListener (node:_http_client:454:9)
at TLSSocket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ENOTFOUND',
code: 'ENOTFOUND'
}
[nodemon] app crashed - waiting for file changes before starting...

Error when starting discord bot with mutiple shards

const config = require('./config.json');
const logger = require('./utils/logger');
const Discord = require('discord.js');
const Statcord = require("statcord.js");
const { token } = require('./utils/variables.js');
const manager = new Discord.ShardingManager('./index.js', {
token: token,
autoSpawn: false,
totalShards: 5,
respawn: true,
spawnTimeout: -1
});
manager.on('shardCreate', shard => logger.info(`Launching Shard ${shard.id + 1}`, { label: `Neo Shard` }));
manager.spawn(manager.totalShards, 5500, -1);
Thats My Code of My Shard File
And I Run A Dashboard On The Same Instance
This Is The Error
2022-05-07 10:42:48 [info] [Neo Shard] Launching Shard 1
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.
at ShardingManager._performOnShards (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\ShardingManager.js:258:75)
at ShardingManager.fetchClientValues (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\ShardingManager.js:245:17)
at Shard._handleMessage (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:338:22)
at ChildProcess.emit (node:events:527:28)
at emit (node:internal/child_process:938:14)
at processTicksAndRejections (node:internal/process/task_queues:84:21)
(process:5992): GLib-GIO-WARNING **: 10:42:53.218: Unexpectedly, UWP app `Clipchamp.Clipchamp_2.2.12.0_neutral__yxz26nhyzhsrt' (AUMId `Clipchamp.Clipchamp_yxz26nhyzhsrt!App') supports 46 extensions but has no verbs
2022-05-07 10:42:54 [info] [Neo Shard] Launching Shard 2
Dashboard is up and running on port 5003.
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.
at ShardingManager._performOnShards (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\ShardingManager.js:258:75)
at ShardingManager.fetchClientValues (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\ShardingManager.js:245:17)
at Shard._handleMessage (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:338:22)
at ChildProcess.emit (node:events:527:28)
at emit (node:internal/child_process:938:14)
at processTicksAndRejections (node:internal/process/task_queues:84:21)
(process:20872): GLib-GIO-WARNING **: 10:42:58.757: Unexpectedly, UWP app `Clipchamp.Clipchamp_2.2.12.0_neutral__yxz26nhyzhsrt' (AUMId `Clipchamp.Clipchamp_yxz26nhyzhsrt!App') supports 46 extensions but has no verbs
2022-05-07 10:42:59 [info] [Neo Shard] Launching Shard 3
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5003
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at Server.listen (node:net:1508:7)
at Function.listen (C:\Teun\NeoSecurity\node_modules\express\lib\application.js:618:24)
at module.exports (C:\Teun\NeoSecurity\dashboard\dashboard.js:5243:9)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1399:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 5003
}
C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:158
reject(new Error('SHARDING_READY_DIED', this.id));
^
Error [SHARDING_READY_DIED]: Shard 1's process exited before its Client became ready.
at Shard.onDeath (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:158:16)
at Object.onceWrapper (node:events:642:26)
at Shard.emit (node:events:527:28)
at Shard._handleExit (C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:384:10)
at ChildProcess.emit (node:events:527:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
Emitted 'error' event on Shard instance at:
at C:\Teun\NeoSecurity\node_modules\discord.js\src\sharding\Shard.js:392:49
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
[Symbol(code)]: 'SHARDING_READY_DIED'
}
I dont know how i can fix this i not that advanced
My dashboard is also in js en ejs.

how to resolve ECONNRESET error in ldapjs?

I am working with LDAP JS. I am receiving ECONNRESET Error again and again, I also tried to browse it but no success. tried to add reconnect: true qualifier but no success. my code and error is as follows.
const adConfiguration = {
url: "ldap://" + process.env.ad_URL,
reconnect: true,
tlsOptions: {
rejectUnauthorized: true,
}
}
Error I am receiving is as follows:
Waiting for the debugger to disconnect...
events.js:353
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
at TCP.callbackTrampoline (internal/async_hooks.js:134:14)
Emitted 'error' event on Client instance at:
at Socket.onSocketError (F:\MFA\CrypLock_BE\CrypLockServer\node_modules\ldapjs\lib\client\client.js:966:12)
at Socket.emit (events.js:376:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
[nodemon] app crashed - waiting for file changes before starting...
You need to listen for the error on your ldap client instance and handle it:
const ldapjs = require('ldapjs')
const adConfiguration = {
url: "ldap://" + process.env.ad_URL,
reconnect: true,
tlsOptions: {
rejectUnauthorized: true,
}
}
const client = ldapjs.createClient(adConfiguration)
client.on('error', (err) => {
console.log(err.message) // this will be your ECONNRESET message
})
I recommend using some sort of abstraction around your raw ldap client so when you detect the error you can reset your client appropriately.

Fetch failed loading: POST "http://localhost:3000/weather"

Terminal Error:
> weather-app#1.0.0 devStart
> nodemon server.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
node:events:355
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1310:16)
at listenInCluster (node:net:1358:12)
at Server.listen (node:net:1445:7)
at Function.listen (/home/ivan-ilyich/Documents/dev/weather-app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/ivan-ilyich/Documents/dev/weather-app/server.js:22:5)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1337:8)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
^C
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ stop
Command 'stop' not found, but there are 18 similar ones.
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ npm stop devStart
npm ERR! missing script: stop
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ivan-ilyich/.npm/_logs/2021-02-26T14_02_05_736Z-debug.log
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ ^C
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ ^C
ivan-ilyich#ivan-Ilyich:~/Documents/dev/weather-app$ npm run devStart
> weather-app#1.0.0 devStart
> nodemon server.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
node:events:355
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1310:16)
at listenInCluster (node:net:1358:12)
at Server.listen (node:net:1445:7)
at Function.listen (/home/ivan-ilyich/Documents/dev/weather-app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/ivan-ilyich/Documents/dev/weather-app/server.js:22:5)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1337:8)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
I'm new to programming and maybe my question is silly but I didn't found an answer from similar questions so I'll give it a try and post my own(sorry for my bad English, it's not my native language).
I'm working on a small tutorial weather app project but I'm forced to make it "my" way and I'm lost with the connection of the server and the client.
I keep getting that error: Fetch failed loading: "http://localhost:3000/weather"
Client side:
const searchElement = document.querySelector('[data-city-search]')
const searchBox = new google.maps.places.SearchBox(searchElement)
searchBox.addListener('places_changed', () => {
const place = searchBox.getPlaces()[0]
if (place == null) {
return
}
const latitude = place.geometry.location.lat()
const longitude = place.geometry.location.lng()
fetch('/weather', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
latitude: latitude,
longitude: longitude
})
})
.then(res => res.json())
.then(data => {
console.log(data);
// setWeatherData(data, place.formatted_address)
})
})
Server side:
if ( process.env.NODE_ENV !== 'production' ) {
require('dotenv').config()
}
const OPEN_WEATHER_API_KEY = process.env.OPEN_WEATHER_API_KEY
const express = require('express')
const app = express()
const axios = require('axios')
app.use(express.json())
app.use(express.static('public'))
app.post('/weather', (req, res) => {
const url = `api.openweathermap.org/data/2.5/weather?lat=${req.body.lat}&lon=${req.body.lon}&appid=${OPEN_WEATHER_API_KEY}`
axios({
url: url,
responseType: 'json'
})
.then(data => res.json(data.main))
})
app.listen(3000, () => {
console.log('Server Started!');
})
Just change fetch('/weather', { as fetch('http://localhost:3000/weather', {
You are hosting your api on port 3000 and trying to send a POST request to port 80

cwd of node child process can't access subfolder

I have problems declaring the path for the current working directory of my child process. I want to execute some R-Scripts and I want to save the results in a folder. But it does not work.
This is working code in my server.js
app.post('/execScript', function (req, res) {
var childProcess = require('child_process');
var project = req.body.project;
var script = req.body.script;
childProcess.exec('Rscript ../app/projects/'+project+'/Scripts/'+script+'',{cwd: '../app/'}, (err) => {
if (err) {
console.error(err);
}
})
});
Note, that the cwd saves all results in the /app folder folder.
When I try the following, it stops working and gives me an error.
app.post('/execScript', function (req, res) {
var childProcess = require('child_process');
var project = req.body.project;
var script = req.body.script;
childProcess.exec('Rscript ../app/projects/'+project+'/Scripts/'+script+'',{cwd: '../app/test/'}, (err) => {
if (err) {
console.error(err);
}
})
});
I basically want to choose a subfolder of /app and it doesn't work.
I get the following error:
{ Error: Command failed: Rscript ../app/projects/das_ist_ein_Test/Scripts/test.R
at ChildProcess.exithandler (child_process.js:211:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:885:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:501:12) killed: false,
code: 2,
signal: null,
cmd: 'Rscript ../app/projects/das_ist_ein_Test/Scripts/test.R' }
Why is that?
Any help would be appreciated !
EDIT:
I made sure that the subfolder exists and that i wrote it correctly.
I got it.
The error occurs because of the way child processes work in node. When given a cwd, you start in that directory and after that you have to move to the file that needs to be executed from you cwd.

Categories

Resources