Using shell.js with cat command in node.js - javascript

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;

Related

Uncaught TypeError: Cannot read properties of undefined (reading 'json_to_sheet')

I am getting this error and do not know what to do. I have not used node_modules before, so it would be nice with help.
my files:
i am using the test.js filecode:
const XLSX = fetch('xlsx')
// array of objects to save in Excel
let binary_univers = [{'name': 'Hi','value':1},{'name':'Bye','value':0}]
console.log(binary_univers)
let binaryWS = XLSX.utils.json_to_sheet(binary_univers);
// Create a new Workbook
var wb = XLSX.utils.book_new()
// Name your sheet
XLSX.utils.book_append_sheet(wb, binaryWS, 'Binary values')
// export your excel
XLSX.writeFile(wb, 'Binaire.xlsx');
What i am trying to do is to have data into xlsx, but for some reason it does not know the function "json_to_sheet".
please comment if you know how to fix it! I am also unsure on what Node.js is, so if that is related please explain to me.
Discord:
Abinesh🦞#6158
UPDATE:
I have done what you said, but i wont still work. DId i do something wrong? (look at the picture)
(i am using the test.js file)
you need to install xlss first to your project, open a terminal and go to you project path and install the xlss liberary
i.e
$ npm install xlsx
other ways to install
once installed, you can use this without any error
const xlsx = require('xlsx');
updated answer:
after looking into your package.json , it's clear it's not configured properly, so follow this stpes
Create an empty folder and open it in terminal.
type npm init -y on the empty folder, once done you should see a proper package.json
type npm install xlsx
create a new file and name is whatever you like (e.g myFile.js)
and paste you excel code there.
make an edit in package.json on the script key, like this
"scripts": {
"start": "myFile.js"
},
Now you run the code and should not see any error, command to run node .\myFile.js

/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

"No Matching Files Found" Running Sigcheck (Node.js) with execSync

I am writing a program in Javascript (Node). In this program I want to get an executable's version number with the Windows Sigcheck tool, but it is giving me "No matching files found."
I can run Sigcheck from the command prompt and it works fine:
Sigcheck finds the executable, and the output is the executable's version number.
But when I run the same command through Node's child_process execSync(), Sigcheck cannot find the file. I run this code:
let appVersionNumber = execSync('\\path\\to\\sigcheck.exe -n "C:\Program Files\Autodesk\Maya2017\bin\maya.exe"');
And I get this output instead:
"No matching files found"
How can I run Sigcheck in Node to get the executable's version number? It is definitely running Sigcheck because it is displaying the banner, but I do not understand why it can't find the executable. Any help would be appreciated- thanks!
You need to escape backslashes in the param string too:
let appVersionNumber = execSync('\\path\\to\\sigcheck.exe -n "C:\\Program Files\\Autodesk\\Maya2017\\bin\\maya.exe"');
Otherwise they'll just be skipped (it's how slashes are treated in string literal in JavaScript), and you'll get the wrong path with no files for sigcheck to, well, check.
BTW, if I were you, I'd probably have put those pathes into separate variables:
const sigcheckPath = '\\path\\to\\sigcheck.exe';
const appPath = 'C:\\Program Files\\Autodesk\\Maya2017\\bin\\maya.exe';
let appVersionNumber = execSync(`${sigcheckPath} "${appPath}"`);

Executing Simple Unix Terminal Commands using Node.js

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.

Where to place JS files for NodeJS to see them

I've just installed NodeJS on my Mac, and i got it working in the terminal, using inline scripting like "console.log('Hello world'); works fine.
But where do i place JS files for NodeJS to find them? Can i specify the root folder NodeJS to look for file in?
I followed this guide: http://nodeguide.com/beginner.html#learning-javascript
but i cannot get any of the samlpe to work where i reference a script file.
You put them in whatever folder you want. It is common practice to put each application in a different folder.
Then you run node.js like this:
node /path/to/file.js
Or like this:
cd /path/to/
node file.js
Where file.js might look something like this:
console.log('hello world');
You'll have to navigate to the correct folder "manually", in the Node Command Line Interface (CLI).
If you need to change drives, type the drive letter and a colon to switch to that drive, like so;
C:> (<- this is the line prompt, yeah? Just add this after it -> D:
That changes the drive. Now write cd (CD = "Change Directory") and the name of the direcotry you want to go to the directory your stuff is in:
D:> (<- the new prompt. Write something like this after it: ->) cd myprosject\subfoldername
D:\myproject\subfoldername> (<- your new line prompt - if "myproject\subfoldername" exists)
now ask node to execute your script (that is stored in myproject\subfoldername, like so;
D:\myproject\subfoldername> node helloworld.js
Remember to write "node" first - otherwise the command won't go to node, but to the OS, which will probably just open up the js file in a text editior instead of running the goodies inside.
It is very easy.. Go to your command line. navigate to the file location..
then simply run the node helloworld.
I'm not sure I understand. it doesnt 'look' anywhere for your .js files you point at them when you run node. Like so, on the command line:
node mynodeapp.js
If you're meaning where does it look for your .js files as modules, when requirign them, like so:
var mymodule = require("mymodule");
Then it will look inside a folder names node_modules. But I'm sure you're looking for my first example above.

Categories

Resources