Executing Simple Unix Terminal Commands using Node.js - javascript

I am trying to execute simple terminal commands through Node.js but so far have only gotten 'ls' to work.. What if I want to change directory etc? Anyone know how to do that?
Here is code that just does the 'ls' command via Node.js.
var exec = require('child_process').exec;
var cmd = 'ls';
exec(cmd,function(error,stdout,stderr){
console.log(stdout);
console.log(stderr);
if(error!=null){
console.log(error);
}
});

Well I'm using a command abtrations for doing thing like that.. You can practically use the cd like in your terminal whit this:
https://www.npmjs.com/package/shelljs
how to use a cd:
var shell = require('shelljs');
shell.cd('../');
and the good thing It's that is an abstraction to the command so It should work on any OS..

First : Try to get the input data from your console
Second : Try to execute it in the node code
Third : Try to get the result and show it in your console
All those steps, you can get the answer from google or docs of nodejs. If you can't find the answer, try to search it in npm.

Related

The term “URL=Test.com” is not recognized

I have the following in my test file. I’m trying to use env variables on my scripts then send the value thru commandline.
const MYURL = process.env.URL;
console.log(MYURL)
In the commandline when I run the following:
URL=Test.com npx playwright test
I get an error message:
The term ‘URL=Test.com’ is not recognized as the name of the cmdlet..etc.
Because you use bash syntax in Powershell. Powershell does not set env variables like var=value, so this syntax won't work.
You probably need to read something like this to figure out the right syntax for Powershell.

/bin/sh: node: command not found - in VScode, running Javascript

Im trying to run this simple code in VSCode for learning Javascript but I keep getting this error:
[Running] node "/var/folders/xr/30nkhmxs7159fblbjtfj2jhw0000gn/T/tempCodeRunnerFile.javascript"
/bin/sh: node: command not found
[Done] exited with code=127 in 0.014 seconds
I've looked online and have tried changing the CodeRunner Executable Map as I saw in another post but it doesn't seem to be helping.
Thanks!
let admin, name; // can declare two variables at once
name = "John";
admin = name;
alert( admin ); // "John"
First of all, check the output of which node in the default terminal application. If the output is empty, this means that the path where the node binary resides is not in your $PATH.
Try to find the location of the node executable. After this, check what's the shell you're using by running echo $SHELL. If it returns something like /bin/bash, create a file(may already exist) named ".bash_profile" or ".bashrc" and there, add the following: export PATH=$PATH:<location of node>, replacing <location of node> with the actual location of the node binary.
in this page: How to run javascript code in Visual studio code? /bin/sh: 1: node: not found
dmcquiggin had resolve this problem:
Locate the path to your Node executable, by typing the following command in a terminal:
which node
The result will be similar to the following (I use nvm to manage my Node versions, yours might look a little different)
/home/my_username/.nvm/versions/node/v10.15.1/bin/node
Make a note of / copy this path.
Open VS Code. Either press Ctrl+, (on Linux), or from the File menu, select Preferences > Settings.
In the search box at the top of this window, type:
Executor Map
Click the 'Edit in settings.json' link displayed under the first result.
Add the following to the end of the settings file, replacing the path with the one from step 1.
"code-runner.executorMap": {
"javascript": "/home/my_username/.nvm/versions/node/v10.15.1/bin/node"
}
this also works on my mac, cheers

Using shell.js with cat command in node.js

I need to use shelljs in Node.js. I write this two lines for getting standart output and writing it into something.xml file by using cat command. But with this style, the stdout will shown in the terminal. I don't want to it. How can I fix it ? Do I need to use something different ? I don't want to anything in the terminal about stdout, I need "stdout" only in a file.
var sh = require('shelljs');
var xyz = sh.cat('./something.xml').stdout;

Node.js - Call a system command or external command

I've an issue with Node.js. With Python, if I wanted to execute an external command, I used to do something like this:
import subprocess
subprocess.call("bower init", shell=True)
I've read about child_process.exec and spawn in Node.js but I can't do what I want. And what's I want?
I want to execute an external command (like bower init) and see its output in real time and interact with bower itself. The only thing I can do is receive the final output but that don't allow me to interact with the program.
Regards
Edit: I saw this question but the answer doesn't work here. I want to send input when the external program needs it.
How about this?
var childProcess = require('child_process');
var child = childProcess.spawn('bower', ['init'], {
env: process.env,
stdio: 'inherit'
});
child.on('close', function(code) {
process.exit(code);
});
Seemed to work for me

How do I handle command-line arguments in a mongo script?

I've been working on some simple scripts to run on mongo from the bash command-line. Originally, I ran them as follows:
$ mongo dbname script.js
but I recently came across mikemaccana's answer, https://stackoverflow.com/a/23909051/2846766, indicating the use of mongo as an interpreter so I can just execute script.js (or any name I choose, with or without the .js) from the command line.
$ script.js
I think it's brilliant and clean, but now I'd like to pass in a database name as a command line argument.
$ script.js dbname
Here I use the bash-style "$1" to demonstrate what I'm doing in script.js.
#!/usr/bin/env mongo
var db = new Mongo().getDB($1);
// Do other things with db, once I resolve the name from the command line.
This results in a "ReferenceError: $1 is not defined ...", which is not surprising. But how would I reference command line arguments? Is this going to be a mongo convention? a javascript convention? Is it possible? It would make my command-line experience with mongo much better aesthetically.
Currently there is no way to do this using the mongo shell...
https://groups.google.com/forum/#!topic/mongodb-user/-pO7Cec6Sjc
... try using a bash script (or other scripting language you are comfortable with) if you want to get a similar command line experience.
Duplicate of How to pass argument to Mongo Script
In a nutshell, this is not possible but several workarounds are given in the answers (not repeated here).
You can pass args to your script through
mongo --eval 'var databasePassword="password"' script.js
and you can access databasePassword value inside script.js
db.auth({ user: 'testUser, pwd: databasePassword });

Categories

Resources