How can I solve 'require is not defined' error? [duplicate] - javascript

This question already has answers here:
What is this JavaScript "require"?
(7 answers)
Closed 8 months ago.
I am trying to make a registration system. When I ran the code on Google chrome, I got an error:
require() is not defined
How can I solve this problem?
//TODO:Register
var mysql=require('mysql');
var con=mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"progmeet_database"
});
function Register(){
//Value Takers
var name_input_value=document.getElementById("Progmeet_register_page_name_and_surname_input").value;
var username_input_value=document.getElementById("Progmeet_register_page_username_input").value;
var Phonenumber_input_value=document.getElementById("Progmeet_register_page_phone_number_input").value;
var DOB_input_value=document.getElementById("Progmeet_register_page_DOB_input").value;
var password_input_value=document.getElementById("Progmeet_register_page_password_input").value;
var email_input_value=document.getElementById("Progmeet_register_page_email_input").value;
//Database appender
var Insert_into_value="INSERT INTO progmeet_user_info_table (name,username,Phonenumber,DateOfBirth,Password,Email),VALUES('namex','usernamex','Phonenumberx','DOBx','passwordx','emailx')";
Insert_into_value.replace("namex",name_input_value);
Insert_into_value.replace("usernamex",username_input_value);
Insert_into_value.replace("Phonenumberx",Phonenumber_input_value);
Insert_into_value.replace("DOBx",DOB_input_value);
Insert_into_value.replace("passwordx",password_input_value);
Insert_into_value.replace("emailx",email_input_value);
con.query(Insert_into_value,function(err,result){
if(err) throw err;
console.log("suscess!");
});
alert("cihantoker")
};
con.connect(console.log("connected!"));

When I ran the code on Google chrome
This code will not run in a web browser. You need to run it on a server via Node. Your browser cannot process require statements, these are part of Node.js.
You can use a pre-processor like Webpack to make require statements work in code that will ultimately run in the browser, but the code you've provided attempts to connect to a MySQL database on localhost. This cannot work in a browser, the code must necessarily run on a server, regardless of whether the require statement can be made to work.

Related

node.js client side errors not showing in console

I am on a school Chromebook, where developer tools are disabled. I sometimes coded on node.js, having server-side and client-side code. But, as developer tools are disabled, I can't check errors on the client-side, as to only find that it sometimes just stops working without a clue as to what is wrong. I have had this issue a whole lot whenever coding client-side code.
How could I detect and identify the error with only access visually to the node.js console, as well as express and socket.io?
For example,
const express=require("express");
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(
'/client',
express.static(__dirname + '/client')
);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html');
console.log("sending site");
});
http.listen(3000, () => {
console.log('listening on *:3000 (http.listen)');
});
io.on('connection', (socket) => {
socket.emit("ERRORNOW",26);
});
//"<script src="/socket.io/socket.io.js"></script>" assumed to be in HTML file
var socket=io();
socket.on("ERRORNOW",()=>{
if("this doesnt have an ending curlybracket){
//}
//this is the error it doesnt have the ending curly bracket,
//but it doesn't show in the node.js console
//(at least on browser IDEs like repl.it),
//and debugging without the developer tools
//can be infuriating to say the least
});
The question is How can I identify a node.js client-side error on a web-based IDE without developer tools?
I have already had this issue a while ago, and due to the lack of developer tools on school Chromebooks, found the solution on my own, but I just thought that maybe I should also put it here.
My solution, as simple as it may be, is to just use a try-catch statement and send the error to the node.js console. It took a while to figure that out...
So, if you don't already have it, you would need a function that when triggered from the client side can log something into the console, inside of the io.on("connection",()=>{}); thing, like so:
io.on("connection",()=>{
socket.on("log", input=>{
console.log(input);
});
});
For example, if everything runs off of a single function that is triggered really fast (specific, but relevant to me, as I make web games), or just off of something, you can run it off of a function inside a try catch, like so:
//"<script src="/socket.io/socket.io.js"></script>" assumed to be in HTML file
var socket=io();
try{
socket.on("ERRORNOW",()=>{
try{
if("this doesnt have an ending curlybracket){
//}
}catch(error){
socket.emit("log",error);
}
});
}catch(error){
socket.emit("log",error);
}
So now, with this simple solution, you can stop commenting out 90% of your code to find that one error that makes it stop working while breaking it further by accidentally commenting out parts that help it to work in the first place!

MATLAB as COM Automation Server with Javascript

I am trying to make a connection between Matlab and a Javascript (typescript in my case) program with a COM automation server as suggested on the MathWorks website. The docs on the website have examples for some languages created by MS, not for javascript.
I can't seem to find a lot of information on how to establish such a COM connection with JS. From what I've read, it's an old Microsoft feature that was only used with Internet Explorer.
Problem
The program I am writing is a VS Code extension, thus I am not using Internet Explorer at all. As a result, I do not believe I can use ActiveXObjects.
Question
Is there another way to establish a connection between my typescript code and the Matlab instance?
Goal
I am trying to run Matlab files from the VS Code terminal without opening a custom Matlab terminal or the complete Matlab GUI. The output should be displayed in the VS Code terminal as well. On MacOS and Linux, I can simply use the CLI tools, but due to the differences between the Windows version and MacOS/Linux versions, this is not possible on Windows.
I haven't used TypeScript very much, and what little I did, it was a long time ago when it was completely new.
However, the NPM package win32ole can be used in NodeJS, so I would assume you would be able to use it in Typescript as well (perhaps with some minor modifications to the example, or a small wrapper).
win32ole npm page
This is an example from that page, showing how to interact with Excel to create and save a worksheet.
try{
var win32ole = require('win32ole');
// var xl = new ActiveXObject('Excel.Application'); // You may write it as:
var xl = win32ole.client.Dispatch('Excel.Application');
xl.Visible = true;
var book = xl.Workbooks.Add();
var sheet = book.Worksheets(1);
try{
sheet.Name = 'sheetnameA utf8';
sheet.Cells(1, 2).Value = 'test utf8';
var rg = sheet.Range(sheet.Cells(2, 2), sheet.Cells(4, 4));
rg.RowHeight = 5.18;
rg.ColumnWidth = 0.58;
rg.Interior.ColorIndex = 6; // Yellow
var result = book.SaveAs('testfileutf8.xls');
console.log(result);
}catch(e){
console.log('(exception cached)\n' + e);
}
xl.ScreenUpdating = true;
xl.Workbooks.Close();
xl.Quit();
}catch(e){
console.log('*** exception cached ***\n' + e);
}

CasperJS not work on Centos Server but work on Windows 10 [duplicate]

This question already has answers here:
Run phantomjs with --ignore-ssl-errors=true from casperjs
(2 answers)
Closed 5 years ago.
This code I wrote on my desktop and ran under Windows 10 and everything works. When I moved to Centos Server, I started to get errors. Help me please
CasperError: Cannot get informations from input[name="email"]: element not found.
/home/casper/casperjs/phantomjs:/platform/casper.js:1158 in getElementInfo
/home/casper/casperjs/phantomjs:/platform/casper.js:1721 in sendKeys
/home/casper/casperjs/phantomjs:/code/cars.js:9
/home/casper/casperjs/phantomjs:/platform/casper.js:1685 in runStep
/home/casper/casperjs/phantomjs:/platform/casper.js:414 in checkStep
var casper = require('casper').create();
casper
.start('https://example.com', function() {
this.echo(this.getTitle());
this.click("span.btn.btn-green");
}).wait(5000).then(function () {
this.capture('sdsdsd.png');
this.sendKeys('input[name="email"]', 'name');
this.sendKeys('input[name="password"]', 'pass');
this.click("button.btn.btn-h40.btn-green.btn-fw.btn-submit");
console.log("Auth");
})
casper.run();
Use the phantomjs option --ignore-ssl-errors=true to byepass the SSL certification validation. For more info https://casperjs.readthedocs.io/en/latest/cli.html#casperjs-native-options

PhantomJS expected an indentifier but found 'local' insted

Hi im insstall phantom js and create local.js file contains next code
var page = new WebPage(),
system = require('system'),
adress
if(system.args.length < 2){
console.log("need adress");
phantom.exit();
} else{
console.log("im running")
phantom.exit();
}
but wwen im runnin code in comand line im have error
phantomjs> local.js
expected an indentifier but found 'local' insted
phantomjs://repl-input:1 in global code
You're not running your script file but you're trying to access a variable called local which would be an object with a property js. When you run phantomjs this way is like a JavaScript console: you're executing JavaScript code.
If you want to run a script file you need to use the command-line interface (CLI) as follows:
phantomjs local.js

terminateAppHandler [base.js] Line 9357 Script

I am developing a windows 8 application. After generating a page on the app (I copied the code from win js (http://try.buildwinjs.com/#listview:gridlayout)) always an error occurs:
terminateAppHandler [base.js] Line 9357 Script
The code that generates this error is:
var terminateAppHandler = function (data) {
debugger;
MSApp.terminateApp(data);
};
I don't know what is meaning of the error it self, however with other pages which also use win js doenst happen.
Any idea why?

Categories