how to run a js file in command prompt - javascript

I have to run a JavaScript file as an executable to show a dialog with standard buttons from command prompt.
I am new to command line programming with java script.

Standard Javascript is not something that natively runs in a command line environment. It is designed for use in browsers. However, Node.JS is a framework built to give you this exact feature of running Javascript as a standalone.
It can be downloaded from http://nodejs.org and installed on most platforms.
Once you have it you can invoke your javascript file by running
$ node <your file name here>.js

You cannot run javascript from the command-line. Usually javascript is executed in a browser.
You can, however, use javascript from server-site (or command-line) using Nodejs.
Have a look here on how to achieve this: NodeJs
Or you can use the REPL (Read-Eval-Print-Loop): Repl

Related

How to run js code with node directly in command line, without using a file? [duplicate]

I need to run javascript code using node.js from C++ application, but I don't want to save code to file before. Is it anyway possible to forward code to node.exe directly, without temporary save it to file?
yes it is. You can use the --eval flag to run code:
node --eval "console.log('hello world');"
Well you can launch node.js and then stuff the script on stdin. How to do that exactly depends on your platform.
For details google how to fork process and get it's stdin with your platform and libraries.

How to send a command to the shell with jQuery Terminal?

I want to build a Web-based Terminal and I want to execute a shell command.
How can I use jQuery Terminal library to send command to shell and print the reply?
So to execute the shell commands you need to have a server code. The simplest way is to use, Apache and PHP. For this, you need to create a PHP script and use one of the functions that allow executing shell commands.
shell.php
<?php
if (isset($_POST['command'])) {
echo shell_exec($_POST['command']);
}
with this server-side script in place, you can write JavaScript code using jQuery Terminal that will send the request to that PHP script and display the results.
$('body').terminal(function(command) {
return fetch('shell.php').then(res => res.text());
});
What else you can do to improve the terminal is to use a unix_formatting.js file that will display any ANSI Escape code that may be generated by the shell in the browser:
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/unix_formatting.js"></script>
If you want to see an example of this that is more advanced, you can take a look at:
jcubic/jsh.php that was created as a single file, PHP shell that looks like a real terminal. It uses few tricks to make it more user-friendly like using unbuffer (part of expect package on GNU/Linux) and other bash tricks to make it display ANSI escapes code for every command. By default ls don't return output in colors.
If you want something even more advanced you can look at Leash Shell project that was created to give access to the shell on shared hosting.
Alternatives are:
websocket.sh which has an example of using jQuery Terminal with Unix shell via Web Sockets written as shell script.
web-console that is PHP shell that uses jQuery Terminal.

Webpack on browser

I am trying to run some javascript code on a browser or on a server.
That includes the ES 6 yarn package and node.js. When I run the command from the terminal it launches a local server on a browser and everything works fine.
But if I try to run the HTML file from the folder or uploading the files onto a server, it doesn't work and it doesn't recognize any of the code I wrote.
How can I fix this?
It sounds like you something like Babel in your terminal step somewhere that transpiles your javascript code and launches a browser to serve the code in memory.
In order to have it work in an html file or on your server, you'll have to transpile your javascript code to use in HTML files.
Browsers do not currently understand all ES6 syntax, almost any ES6 javascript code needs to be transpiled for the browser as of today.

Project type for executing JavaScript in IntelliJ IDEA

Is there any IntelliJ IDEA project type that allows me to execute JavaScript, showing the results in the console?
I've been trying HTML5_Boilerplate, but it requires to generate the HTML with the embedded script, for instance.
Thank you.
UPDATE: IDEA can actually do it by taking advantage of Node, no need for an external browser, as long as IDEA's console can do the trick:
How can I set this up from scratch?
IDEA itself can't execute Javascript, it can only be executed by browsers/Node.js interpreter. Project type doesn't matter. If you like to be able to run .js files with Node.js via Run command in right-click menu, you need to make sure that NodeJS plugin is installed and enabled. You can install it via Settings | Plugins, Install JetBrains plugin...

Are all node.js modules server-side?

Is there front-end node modules or all are server-side unless packaged using browserify and similar tools?
Node.js modules are designed to be used with Node.js.
You might run Node.js as a server side environment. You might run it as a command line program. You might run it as an HTTP client in its own right.
Node.js does not run inside a browser. Node.js modules are not generally designed for use embedded in a webpage using <script> elements.
It is possible to write a hybrid JavaScript file that can function both as a Node.js module and as a script in a webpage … but there isn't often a good reason to do so. (I've done it once: To write a client for a particular webservice that I wanted to use in a browser and in a Node.js program).
Browserify can convert some Node.js modules so they can run in a browser.

Categories

Resources