Execute Javascript using Node in terminal - javascript

I know that
Node.js allows to write JavaScript code that runs directly in a
computer process itself instead of in a browser
Reading this, I try to open my terminal and run:
console.log('hello')
but I get zsh: unknown file attribute.
The same happens with node console.log('hello').
If I instead create a file hello.js and put the console inside it and then I run:
node src/hello.js
it works. Why? What am I missing?
Sorry for the (maybe) stupid question

It's because the console.log('hello') is being evaluated by your shell (zsh) before running the command and it has no idea what console.log('hello') is. You could use the -e or --eval arguments.
$ node -e "console.log('hello')"

If your using linux, first type node It will profile for you a REPL. which is essecially the excution terminal.
You can also do the same on windows

Related

Stripe is not recognized by the terminal in VS CODE

Upon running 'stripe listen' command in terminal in VS Code, it throws an issue :-
The term 'stripe' 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.
Can anybody help me in resolving this?
I have already configured Stripe-CLI for my project using command prompt but it is not working in the terminal of vs code.
You can restart your vscode and then try running your command, if it is running in command prompt outside of vscode. VScode terminal uses available system CLIs like command prompt,powershell & others, so whatever is running outside of vscode terminal, should run in vscode terminal as well.
If your command is not running outside of vscode as well, you can check the PATH variable in your system environment variables (search for environment variables in start menu, assuming Windows OS), whether it contains the path to the folder containing stripe executable file or not. You can add the path if it is not present, then restart your terminals/vscode and try running your command.

Open new terminal in MacOSX and run node script

I have been trying to figure out how to open a new terminal window from a Node application and run a preset command or script there.
I can open the terminal window:
const { exec } = require('child_process')
exec('open -a Terminal ' + process.env.HOME)
And this works fine, I have done a bit of research but it seems I can only find examples directed to bash
Could anyone help me on this?
Thanks
One way to do this would be to place the commands you want to run in a shell script with the .command extension. By default, these files will be opened in the Terminal application on macOS. This is a bit of a workaround, but I think it achieves what you would like to do.
Say we have the file hello.js containing the following:
console.log("Hello World")
And the shell script run_node.command
#!/bin/bash
node hello.js
Running the command open run_node.command will open a new Terminal.app instance and run the shell script, which in turn runs the node command. The terminal will close when the script exits.

node.js "Command not found"

I am attempting to work with node and learning how to write in Javascript. I am currently trying to use my terminal to run a basic "Hello world" js file using node. However, when I attempt to use $node index.js (that's what the file is called). All the terminal responds with is index.js command not found. I tried to do it with the just the terminal window, I have also tried to run it through the integrated terminal within data studio, so I am unsure of where I went wrong.
Here is a screen shot of my window.
Remove the $ sign. In web examples, docs use $ to show that you run it in the command line.
Instead use node index.js, or set up a package.json file and use npm start (npm start should contain node index.js).
Final Clarification (if you didn't understand the above explanation)
Run node index.js. $ is never used, except to show you that the command is used in the command line.

Force node.js to use bin/bash instead of sh

I'm trying to execute a basic bash script from node using the exec() function. The bash script is as follows:
#!/bin/bash
ffmpeg -f concat -i <(for f in $1/*.mov ; do echo "file '$f'"; done) -c copy $1/output.mov
The script works fine running it from the command line but when running from within node I get a syntax error: line 2: syntax error near unexpected token('`
It appears when running this command node it attempting to use sh instead of bash. Can anyone verify this is true and give a possible workaround or solution? Thanks ahead of time!
Try child_process.execFile or just explicitly run ['/bin/bash', '/path/to/your/script.sh', arg1, arg2...].
Provided you're on a recent version of Node, you can add the following as option for exec:
{shell: "/bin/bash"}
See: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Install Node.js

I'm completely new to node.js. By reading the documentation and tutorials, I managed to download and install node.js on my windows.
How do I test it and make it work?
The file test.js is saved in the same directory as node.exe and contains:
console.log('Hello World');
Openning the command-line I typed:
$ node test.js
But nothing hapenns, just:
...
You are typing node test.js in the Node REPL not the command line. The ... is indicating that you haven't reached the end of a valid statement yet (because you are writing shell and not JavaScript).
Run a command line with your terminal emulator of choice (probably Windows Powershell if you are using Windows).
Run the Node REPL by executing node via the command-line without any arguments. The reason you're not getting the expected results is probably because you're running node.exe directly. Since you're using windows, start up CMD and run node.exe from there. Once you have the REPL running, try node test.js again, and this time it will work.
![enter image description here][1]I have run it in nodejs shell and it is simply working
> console.log('Hello World');
Hello World
undefined
>
Also I opened nodejs command prompt not default command prompt of windows and then
node path_to_file
it is working... same output

Categories

Resources