I have a simple JavaScript myScript.js with code as below;
var printer = function(someString){
console.log(someString);
}
printer("This is printed on console.");
I'm on Windows 10 and need to be able to call myScript.js on a command prompt and have the code executed without doing node myScript.js. Is there a way to set up things so that Command prompt or PowerShell can automatically call Nodejs or other JavaScript engine?
If your Windows can run normal shell scripts on the command line then you can add a shebang line to your Node script just like you do with shell scripts. For example:
#!/usr/bin/env node
// your node code here
Also you can try to configure Node to be invoked for all files with the .js extension but this can be a security hazard because you may execute arbitrary code just by clocking on JavaScript files that you may have downloaded on your system so I wouldn't really recommend that.
Another alternative is to make a BAT script for you Node app:
example.bat:
node example.js
example.js:
// your Node script
That you will be able to run with just:
example
on your command line.
Related
I'm trying to build a web app where user can write the JavaScript code and save that JS code in form of string for later execution.
Trying to run the script saved as a string in backend , then fetch that script later and run through NodeJS.
Yes it can. But it is inefficient and insecure. I prefer to import the javascript file by using require or import.
#!/bin/bash
# Get the name of the file to run from the command line arguments
filename="$1"
# Rename the file to have a .js extension
mv "$filename" "${filename%.txt}.js"
# Run the renamed file with NodeJS
node "${filename%.txt}.js"
you can write a shell script in linux, where you point the .txt(extension) file to rename it into .js(extension) and you can auto run it once your Linux os has Nodejs install.
You can use VIM editor in linux for writing a shell script for better feel.
Following is the steps for above program's executions
Save this script in a file with a .sh extension, for example runscript.sh. Make sure the file has execute permissions, you can grant it by running chmod +x runscript.sh in the command line.
Then, to run your .txt script, navigate to the directory where it is saved and run the shell script, passing the name of the .txt file as a command line argument:
./runscript.sh myscript.txt
Now this you can run this file
Please excuse my limited K6/Javascript knowledge.
I have been given a javascript file that can be used to create a batch of users to be used within my k6 load test.
The script is currently ran from the commmand line using this command:
node helpers/createUsers.js 10
I want to run this script in the 'setup' function in K6.
Can I run node commands directly in K6 as above, or do I have to turn it into an export function and then call it?
To my knowledge, you cannot run nodeJS or any nodeJS modules directly from a k6 test script. If the script helpers/createUsers.js is using any node modules, then you should follow this. and have the script in the setup() function.
I'm running on Windows Server 2012R2.
I have a node.js script which I want to run when the user performs logins.
To do this I'm setting the command to run in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry path.
I set this command:
/path/to/node/node /path/to/node/script args
This works fine, but it will spawn a terminal showing the command output, while I'd like to run this command in background and detached from any command terminal.
I tried to replace the previous command with:
start /b /path/to/node/node /path/to/node/script args
but in this case there is no evidence that the script was even started.
I also tried to wrap the following command in a .bat script:
start \b node script args
set the name of the script in the Run key: in this case I can see terminal flash but then the script is not running anymore (I suppose that the script is executed but then it is stopped as soon as the parent process is terminated).
I want to avoid to convert the script in a windows service, as long as a simpler solution is possible.
A solution not using the Run registry key is also fine, as long as it fulfils my requirement (run a script in background when the user logins).
Actually it's pretty easy , use forever.js module
after installing the module use
"forever start main.js"
It will start running as your background process
I have installed mean stack manager, but when I run command using node terminal console.log("Hello World") it works fine, but whenever I put Javascript sample file to any folder or anywhere in the mean stack install directory or install modules every it don't console the file, so I need your suggestions to where I have to put the Javascript file in mean stack manager to console the file.
Bitnami MEAN STACK MANAGER
Bitnami developer here.
The easiest way to play with node is using its interactive shell. Go to your installdir (in your case D:\New Programmes\New Server), double click on use_meanstack and execute:
$ node
Then, you should see > indicating that node is waiting for instructions.
You can also write all the instructions in a file with js extension (sample.js) execute it directly using:
$ node sample.js
You can place this file anywhere you want. By default, Meant Stack working directory is installdir, so you can place you file into D:\New Programmes\New Server to test it directly writing $ node sample.js. However, I think it would be a good pratice to place it under nodejs folder (in your case D:\New Programmes\New Server\nodejs). If you put sample.js there, you should execute
$ node nodejs\sample.js
I hope it helps
How can I run shell command in javascript in code behind
I have this exe file on:
C:\Program Files\test.exe
that i want to run using javascript and I want to do it in codebehind or at least call this function from code behind.
The reason want to do it on code behind is that I have parameters need to pass them in the shell command.
For security reasons you can not run commands from JavaScript.
If you are using JScript, then
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("command here");