54

I have several JS files so instead of copy and paste every one in the console window (Firefox and Chromium), I would want to call them from the shell if it is possible.

Every JS file has test functions that show if they are correct using console.log.

Beau Smith
  • 29,103
  • 12
  • 82
  • 88
  • In Chrome Developer Tools, you can have [Content Scripts](http://developer.chrome.com/extensions/content_scripts.html). – Jared Farrish Sep 30 '12 at 08:30
  • What do you mean by "console window", is this associated with a webpage? – Bergi Sep 30 '12 at 09:18
  • I'm using console.log to write messages in the console of Firefox/Chromium, but I can use Document.write instead. –  Sep 30 '12 at 09:24
  • Possible duplicate of [Executing JavaScript without a browser?](https://stackoverflow.com/questions/2941411/executing-javascript-without-a-browser) – kenorb Aug 20 '18 at 16:10
  • Firefox headless mode, bro https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode – Stack Underflow Sep 13 '19 at 22:03

6 Answers6

83

Expanding upon the solution to use Node.js…

Here are some examples and screenshots from a page on Command Line JavaScript.

The Node REPL (Shell)

If you enter node on the command line with no arguments, you'll be in the Read-Eval-Print-Loop, or REPL for short, otherwise known as a shell. Here you can interactively enter JavaScript expressions and have them immediately evaluated.

Node REPL (Shell)

Evaluate a JavaScript file from the command line

Create a file with the following content:

console.log('Hello, world');

From the command line, use node to evaluate the file:

Evaluate a JavaScript file

Beau Smith
  • 29,103
  • 12
  • 82
  • 88
17

If your tests need access to a DOM there is always PhantomJS - a headless (Webkit) browser.

Matt
  • 3,588
  • 2
  • 24
  • 38
13

I am not saying that it is the best solution but it is one of the available options. I just want to spread awareness and one reason this is how Java runs javascript because it has an embedded JavaScript runtime already for a long time. First there was Rhino, and now, Java SE 8 shipped with a new engine called Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance through invokedynamic-bound call sites. It can be used to run JavaScript programs from the command line. To do so, builds of Oracle’s JDK or OpenJDK include a command-line tool called jjs. It can be found in the bin/ folder of a JDK installation along with the well-known java, javac, or jar tools.

The jjs tool accepts a list of JavaScript source code files as arguments. Consider the following hello.js file:

var hello = function() {
  print("Hello Nashorn!");
};

hello(); 

Evaluating it is as simple as this:

$ jjs hello.js
Hello Nashorn!
$

For more detail you can refer official documentation http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html

Rohit Luthra
  • 1,106
  • 14
  • 25
7

Yeah, It can be possible with node. Just create a js file, write some code, save that and go to the directory where you have saved your file, then enter node <your file_name>. You are done. Note: You must have node installed on your system.

6

You can do this using node.js. You can run each file separately, but of course I'm assuming there's no dependency between files.

This windows command line javascript discusses using Windows Scripting Host if you're on Windows and don't want to install Node. But Node is probably the better bet for standardized js (it uses the v8 Javascript engine).

Community
  • 1
  • 1
craigmj
  • 4,246
  • 1
  • 16
  • 21
  • There's also the [Spidermonkey](https://developer.mozilla.org/en-US/docs/SpiderMonkey) and [Rhino](https://developer.mozilla.org/en-US/docs/Rhino) JavaScript interpreters from Mozilla. – glenn jackman Sep 30 '12 at 10:21
1

For those in hurry and on Windows, just try the following commandline in the command prompt:

cscript /E:jscript myJavaScriptFile.js
primehunter
  • 215
  • 3
  • 8