Error While caling Python function from Node.js express - javascript

Tried searching forms , but could not find solution, Any help here is much appreciated.
Trying to call Python from node express , but getting below error.
var PythonShell = require('python-shell')
PythonShell.run('script.py', options,function (err) {
if (err) throw err;
console.log('finished');
});
OR
PythonShell.run('script.py',function (err) {
if (err) throw err;
console.log('finished');
});
ERROR
function extend(obj, ...args) {
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (P:\html\testPy.js:1:81)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)

Related

NodeJS gui module not found

I attempting to use node.js with nodegui module but i got this error
node:internal/modules/cjs/loader:1189
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: The specified module could not be found.
\\?\C:\Users\Kacper\Desktop\sot\node_modules\#nodegui\nodegui\build\Release\nodegui_core.node
←[90m at Object.Module._extensions..node (node:internal/modules/cjs/loader:1189:18)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Module.require (node:internal/modules/cjs/loader:1005:19)←[39m
←[90m at require (node:internal/modules/cjs/helpers:102:18)←[39m
at Object.<anonymous> (C:\Users\Kacper\Desktop\sot\node_modules\←[4m#nodegui←[24m\nodegui\dist\lib\utils\addon.js:3:15)
←[90m at Module._compile (node:internal/modules/cjs/loader:1105:14)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m {
code: ←[32m'ERR_DLOPEN_FAILED'←[39m
index.js code
const { QLabel, QMainWindow } = require("#nodegui/nodegui");
const win = new QMainWindow();
const label = new QLabel(win);
label.setText("Hello world");
label.setInlineStyle("color: green; background-color: white;");
win.show();
global.win = win;
node v16.16.0
npm 8.11.0
windows 10 20h2

Getting MODULE_NOT_FOUND error when using require() for a .js file in sub directory

Using ExpressJS, when I run my project using 'node server.js' command, I get the following error:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '../config/db.config.js'
Require stack:
/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/server.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:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js'
]
}
This is my code inside the db.js file:
const mysql = require("mysql");
var dbConfig = require("../config/db.config.js");
// Create a connection to the database
const connection = mysql.createConnection({
host: dbConfig.HOST,
user: dbConfig.USER,
password: dbConfig.PASSWORD,
database: dbConfig.DB
});
// open the MySQL connection
connection.connect(error => {
if (error) throw error;
console.log("Successfully connected to the database.");
});
module.exports = connection;
Here is the directory structure of the project.
I have also tried this path: './config/db.config.js' but I still get the same error.
Can someone please help?

Error: Cannot find module in node js

I want to add logInPageTest.js in a file with the require field, but when I am adding it , it is throwing this error.
module.js:472
throw err;
^
Error: Cannot find module './test-cases/logInPageTest.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/app/PycharmProjects/AutomationAppTesting/automationTest/index.js:15:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
Hierarchy is as follows:
-AutomationAppTesting
--automaitonTest
---test-cases
----logInPageTest.js
---index.js
Here is my code in index.js file:
require('./test-cases/logInPageTest.js')
Code in logInPageTest.js file:
var wd = require('wd');
var assert = require('assert');
module.exports = function (browser, next) {
maximizeWindow(browser, next);
}
function maximizeWindow(browser, next) {
browser .windowHandle( function (err, handle) {
browser.maximize(handle);
})
}
So, How should I give path to solve the error.

Cannot find module 'require'

this is the error below
E:\freelancer-work-backup\scraping>node index.js
Screenshot of error
$ node index
module.js:341
throw err;
^
Error: Cannot find module 'require'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (E:\freelancer-work-backup\scraping\index.js:1:83)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
Code in index.js:
var request = require('require'),
cheerio = require('cheerio');
var url = 'grocermax.com/catalogsearch/result/…';
request(url, function(err, resp, body) {
if (!err && resp.statusCode == 200) {
console.log(body);
} else {
console.log("Failed");
}
});
You should change the code to var request = require('request')

Use of Google Prediction API in node.js: Error: trainedmodels is undefined

Above code was written in intellij IDEA trying to implement Google Prediction API.
var prediction= require('../googleapis/apis/prediction/v1.6');
var params = {
project: "693608054772",
resource: {"id": "predictiondemo-1054", "storageDataLocation": "prediction3/language_id.txt"}
};
prediction.trainedmodels.insert(params, function (response) {
if (err) {
console.log("response occured:", response);
}
});
And Error that comes out is the following:
/usr/local/bin/node predictionbhola.js
/home/bhola/projects/nodejs/applane/node_modules/MyTests/predictionbhola.js:8
prediction.trainedmodels.insert(params, function (response) {
^
TypeError: Cannot read property 'insert' of undefined
at Object.<anonymous> (/home/bhola/projects/nodejs/applane/node_modules/MyTests/predictionbhola.js:8:25)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Process finished with exit code 1.

Categories

Resources