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
Related
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.
I am trying to make a button open an exe file in computer but it doesn't open and it gives me this error
Uncaught ReferenceError: require is not defined
at runExe (main.js:61)
at HTMLButtonElement.onclick
here is the code for my button
<button onclick="runExe()" id="button">click</button>
and i got this code form the internet and when i remove the function runExe() the exe file opens when i start the app and i want it to only open when button is clicked.
here is the code to open the exe file
function runExe(){
var child = require('child_process').execFile;
var executablePath = "winRAR.exe";
child(executablePath, function(err, data) {
if(err){
console.error(err);
return;
}
console.log(data.toString());
});}
The error message says you cannot use require.
You are trying to run that code in the renderer process. If the distinction between main and renderer is new, see https://www.electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
One way to do what you want is to instead have the rendered process send the open request to the main process, where the require will work, and where opening a child process should work.
BTW, I don't know if shell.openItem() (https://www.electronjs.org/docs/api/shell) can be used to start any exe? If so, that might be the better way to do it.
I'm using CasperJS to test my webApp, the thing is that I need to access a DB to automatize some necessary inputs from my tests.
I'm looking for an alternatives on how to retrieve this data from the DB inside a casperJS js script an finally I decide to use phantomJS child process module to call a groovy script to connect a DB and make a select and print the result to stdout to get it from CasperJS. However from the sample of the phantomJS can not realize how to do it, based on the sample I made some attempts with spawn and execFile with no luck. i.e I try:
var process = require("child_process")
var spawn = process.spawn
var execFile = process.execFile
var child = spawn("groovy", ["script.groovy"])
child.stdout.on("data", function (data) {
console.log("spawnSTDOUT:", JSON.stringify(data))
})
child.stderr.on("data", function (data) {
console.log("spawnSTDERR:", JSON.stringify(data))
})
child.on("exit", function (code) {
console.log("spawnEXIT:", code)
})
This doesn't work and not produce any output. I also try directly executing dir command directly and also nothing happens.
I try also with linux and it doesn't work either, I try also creating a simple echo .sh and nothing..., however in linux when I try to run ls command this times it works as expected.
After some tries I found a way to do it.
Seems that in windows the only way to do it is passing cmd.exe as command and groovy script.groovy as argument.
So I use
var child = spawn("cmd.exe", ["/k","groovy script.groovy"])
instead of:
var child = spawn("groovy", ["script.groovy"])
This way works correctly on windows.
I also found the way to run a shell script on linux, which executes the groovy.It's similar to the windows solution, instead of invoke .sh I've to use sh command:
var child = spawn("sh", ["script.sh"])
And script.sh executes the groovy script:
#!/bin/bash
groovy script.groovy
I am creating a web app in node.js and golang. I need to connect nodejs with golang code which talks to mongodb and returns data to node program. is there any way to connect so? I tried to use gonode API.This is my code using gonode API.
my node.js file contains below code:
var Go = require('gonode').Go;
var options = {
path : 'gofile.go',
initAtOnce : true,
}
var go = new Go(options,function(err){
if(err) throw err;
go.execute({commandText: 'Hello world from gonode!'}, function(result, response) {
if(result.ok) {
console.log('Go responded: ' + response.responseText);
}
});
go.close();
}); `
And this is the code in my gofile.go file:
package main
import(
gonode "github.com/jgranstrom/gonodepkg"
json "github.com/jgranstrom/go-simplejson"
)
func main(){
gonode.Start(process)
}
func process(cmd *json.Json) (response *json.Json) {
response, m := json.MakeMap()
if(cmd.Get("commandText").MustString() == "Hello") {
m["responseText"] = "Well hello there!"
} else {
m["responseText"] = "What?"
}
return
}
This is the error am getting while running as node node.js in terminal
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:905:11)
at Object.afterWrite (net.js:721:19)
Golang from 1.5, you can build go to shared object binary file (*.so). This allows you to connect your go compiled library to be called by nodejs, python, ruby, java etc.
Here is a guide you could refer to: https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf
thanks for the response. I got a solution for this. I made 2 different servers. One for NodeJS and another for Golang. I am calling golang uri in Node server and getting data from golang server.
Based on a very cursive check of the gonode source code, the module seems to spawn go code as a child process and communicate through stdin/-out. EPIPE error means that the other end closed the stream. Based on this it might be that your go process exits prematurely.
You could try to debug the problem by modifying Command.prototype.execute in gonode/lib/command.js to print out the JSON that's sent to the go process. Then you can debug the go program by running it directly and giving it the same input via stdin.
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?