I am very new to coding, and have just installed VS Code, and installed Node.js as well as Git Bash. I was working on a project, but couldn't console.log anything as I always received the following error when using Git Bash:
bash: syntax error near unexpected token `validateCred' (One of the
variables I used)
I created a new JS file, and ran the following code:
const hello = 'hello world';
I still received the same bash error. I have tried replacing my code with code that I know works and still receive the same errors.I have also tried using Powershell as my terminal, but receive the following errors every time:
batch : The term 'batch' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
I am at a loss, and I'm sure it's something really simple, but I can't seem to figure it out!
A Bash shell expects you to enter Bash code and not JavaScript code.
If you want to run JavaScript code then you need to run it in Node.js and not in Bash.
Generally, the command node will launch Node.js in a Bash shell. (Assuming it is installed on the $PATH).
I'm running node.js on Ubuntu 18.04 LTS. I appear to be getting different behaviour using require depending on whether I use it in the REPL or in a script. Specifically, I used npm to download lightstreamer-client-node. Now, I open up a terminal and do the following:
colin#colin-XPS-15-9550:~$ node
> var x = require('lightstreamer-client-node')
This works perfectly.
Now I want to use this package in a script. I create a text file node_test.js containing just the line:
var x = require('lightstreamer-client-node')
and I open up a terminal and run the command:
colin#colin-XPS-15-9550:~$ node /home/colin/node_test.js
This hangs indefinitely on a blinking cursor.
I'm brand new to node.js and JavaScript so perhaps this is expected behaviour. I've done some reading about the require function and can't seem to find an explanation for it. Note that if I replace lightstreamer-client-node with some other node module, e.g. safe-buffer, then everything works fine, whether I use REPL or script.
The process seems to hang because the library lightstreamer-client installs a timer, with the function setInterval, for its internal activities, and nodejs doesn't allow a graceful shutdown when there are active tasks. So the only way to terminate the script is by using the function process.exit.
I have a JavaScript code that I need to run on chrome console. I can upload it and run it on Chrome, but I'm trying to automate that by writing a Python script that does the same. Here is what the python script should do:
1) Take in input from the user, using Command Line arguments in python. e.g.:
python FileName.py --from www.abc.com --to www.def.com
2) Update 2 variables in said DoSomething.JS as:
var from_site = "www.abc.com"
var to_site = "www.def.com"
3) Upload the JS file to chrome and run it in console.
4) The JS file does its job and downloads something. Parse the data through python (I can do this) and run some other commands in said chrome console, all through the same script.
Is there a library I can look up in order to do this? I'm trying to run the python script locally on my laptop.
Using windows my code I currently use:
var file = JSON.parse(fs.readFileSync(filePath), 'utf8');
file[id] = JSON.parse(`{"name":name}`);
fs.writeFileSync(filePath, JSON.stringify(file,null,2);
This works just fine on windows. However, when I transfer this code onto my linux machine for cross-platform testing. The file doesn't get updated until I stop running the application.
What i've tried:
running 'chmod -r 0777 path/to/dir/'
and
checked if it was due to max file open limit (not the case as my limit is extremely large)
Linux: Mint-Linux 4.10.0-38-generic #42~16.04.1-Ubuntu (from uname -a)
Node: 8.10.0
Editor: intelliJ idea
Im also running the code directly from the run button in intelliJ, im not sure if this may be the cause, if it is the cause why would this be the case.
Adding the option {mode: 0o777} as the last arg in the write fixed the files not updating.
I'm trying to debug my JavaScript scripts, which are running in V8, with node-inspector. On the app side, I just did
v8::Debug::EnableAgent("MyApp", 5858);
Node-inspector connects fine and even is able to pause/unpause and to show the code. However, step-wise execution does not work, neither breakpoints and probably a lot of other things. When I try to do such things, I get these errors from Node-inspector:
Node Inspector v0.7.0
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setBreakpoint
Received request for a method not implemented: Debugger.setBreakpoint
Received request for a method not implemented: Debugger.setBreakpoint
So I guess I'm missing something.
I'm not sure if what I'm trying to do is even supported - because I guess, node-inspector is meant for Node.js and not for arbitrary V8, right? If so, what would be needed to make it work?
Thanks for the help by Miroslav, esp. to run DEBUG=node-inspector:protocol:* node-inspector, I got a bit further. Step-wise execution works now, and breakpoints do to in most cases (except when you select the wrong Source file - see below).
I provided a global process object like this:
// process object: http://nodejs.org/api/process.html#process_process
process.stdout = ...
process.stderr = ...
process._baseDir = ...
process.mainModule = {filename: process._baseDir + "/main.js"}
process.argv = ["myapp.exe", process.mainModule.filename]
process.cwd = function() { return process._baseDir; }
Now I get the error Internal error: TypeError: Cannot read property 'line' of null in the console. In node-inspector, I get this:
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:v8-debug request: {"seq":170,"type":"request","command":"backtrace","arguments":{"inlineRefs":true,"fromFrame":0,"toFrame":50,"maxStringLength":10000}}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:devtools frontend: {"method":"Debugger.setOverlayMessage","id":48}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:devtools backend: {"id":48}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:v8-debug response: {"seq":41,"request_seq":170,"type":"response","success":false,"message":"Internal error: TypeError: Cannot read property 'line' of null"}
Another thing is that the script files are not always correct. On the C++ side, I'm loading them now like this:
ReturnType execJsFile(const std::string& jsSourceDir, const std::string& extfilename) {
v8::TryCatch try_catch;
std::string fullfilename = jsSourceDir + "/" + extfilename;
std::string sourceStr;
CHECK_RETURN(readFile(fullfilename, sourceStr));
// The origin is for the debugger, e.g. node-inspector. It expects an URL.
Local<String> origin = jsStr("file:///" + fullfilename);
Local<String> source = jsStr(sourceStr);
Local<v8::Script> script = Script::Compile(source, origin);
if(script.IsEmpty()) {
assert(try_catch.HasCaught());
return "JS compile failed: " + jsReportExceptionToString(Isolate::GetCurrent(), &try_catch);;
}
Local<Value> result = script->Run();
if(result.IsEmpty()) {
assert(try_catch.HasCaught());
return "JS script execution failed: " + jsReportExceptionToString(Isolate::GetCurrent(), &try_catch);
}
return true;
}
That puts all files under the file:// domain in the Sources list. However, main.js gets an extra entry under (no domain). When I make the change
process.mainModule = {filename: "file:///" + process._baseDir + "/main.js"}
it goes away, however, that is not how I would have expected ìt to be according to the doc.
When I pause/break the execution in main.js, it shows up in yet another Source [VM] main.js and gets a yellow-ish background.
Also, all files in Sources under file:// get the (function (exports, require, module, __filename, __dirname) { prefix added in the first line of the source. That line does not come from my code but from node-inspector. Why does it add that here? It is esp. strange because my code adds a slightly different prefix ( function(module, exports) {.
Run DEBUG=node-inspector:protocol:* node-inspector and inspect the messages, you might be able to find more information there. You can also try to use an older version, e.g. 0.1.9, it may have less dependencies on Node-specific stuff.
I'd say 95% of Node Inspector code uses V8 protocol only. Look for usages of DebuggerClient.prototype.evaluateGlobal to find where Node-specific functionality is used.
The first thing to change is getResourceTree in lib/PageAgent.js. Either implement your own way of listing all source files (including those not loaded yet), or return an empty tree.
UPDATE
Try Node's CLI debugger first:
$ node debug localhost:5858
To my best knowledge, the CLI debugger uses only the V8 debugger protocol features, nothing Node specific. When you are able to debug your V8 app using the CLI, you will know that any other problems are in Node Inspector and not in your V8 app.
Here is a solution for debugging V8 with node-inspector:
First of all, the latest version (0.12.3) has many extensions for debugging node.js scripts so I used an older version (0.5.0) which has less liability to node.js.
I used the following command to install v0.5.0 in Node.js command prompt:
npm install node-inspector#0.5.0
It is installed in folder "%USERPROFILE%\node_modules" by default.
I added one line of code to %USERPROFILE%\node_modules\node-inspector\lib\PageAgent.js:
getResourceTree: function(params, done) {
return; //return empty tree
...
This completes the installation.
Here is the guide to debug a javascript file executed by using the ClearScript .NET library:
Open a command prompt and execute the following command:
%USERPROFILE%\node_modules\.bin\node-inspector.cmd
You should see the following lines if node-inspector is running successfully.
Node Inspector v0.5.0
info - socket.io started
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Initialize your V8 engine as in the following code line (VB.NET code):
Dim engine As New V8ScriptEngine("global", V8ScriptEngineFlags.EnableDebugging, 9222)
Put a breakpoint right after this line in your code and run your application to reach this breakpoint.
Make sure you have "debugger;" statement in the first line of your JavaScript code.
Open a Chrome browser and navigate to the following address:
http://127.0.0.1:8080/debug?port=9222
Press "Continue Debugging" button in Vİsual Studio toolbar.
Now you should see your script code stopped at the first "debugger;" line in Chrome browser.
You can continue debugging from here.