How do you run JavaScript script through the Termi

2019-01-09 22:33发布

For instance, if you were to run a Python script you would type python filename.py or if you wanted to run a C program make filename then ./ filename. How do you do this with .js files?

13条回答
虎瘦雄心在
2楼-- · 2019-01-09 23:04

On Ubuntu, install a link to install libjavascriptcoregtk-3.0-bin and use /usr/bin/jsc (manpage).

查看更多
贪生不怕死
3楼-- · 2019-01-09 23:10

You would need a JavaScript engine (such as Mozilla's Rhino) in order to evaluate the script - exactly as you do for Python, though the latter ships with the standard distribution.

If you have Rhino (or alternative) installed and on your path, then running JS can indeed be as simple as

> rhino filename.js

It's worth noting though that while JavaScript is simply a language in its own right, a lot of particular scripts assume that they'll be executing in a browser-like environment - and so try to access global variables such as location.href, and create output by appending DOM objects rather than calling print.

If you've got hold of a script which was written for a web page, you may need to wrap or modify it somewhat to allow it to accept arguments from stdin and write to stdout. (I believe Rhino has a mode to emulate standard browser global vars which helps a lot, though I can't find the docs for this now.)

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-09 23:10

I tried researching that too but instead ended up using jsconsole.com by Remy Sharp (he also created jsbin.com). I'm running on Ubuntu 12.10 so I had to create a special icon but if you're on Windows and use Chrome simply go to Tools>Create Application Shortcuts (note this doesn't work very well, or at all in my case, on Ubuntu). This site works very like the Mac jsc console: actually it has some cool features too (like loading libraries/code from a URL) that I guess jsc does not.

Hope this helps.

查看更多
smile是对你的礼貌
5楼-- · 2019-01-09 23:13

This is a "roundabout" solution but you could use ipython

Start ipython notebook from terminal:

$ ipython notebook

It will open in a browser where you can run the javascript

enter image description here

查看更多
【Aperson】
6楼-- · 2019-01-09 23:13

All the answers above are great, I see one thing missing and could be considered for running javascripts(*.js) files, the unrelated brother of javascript the Java.

JDK comes up with two nice tools, could be utilized for executing javascripts. Here are command goes like. Make sure to navigate to JDK\bin.

 jjs example.js

Its comes up with another commmand tool that goes like this-

 jrunscript example.js

I hope this may be helpful to others.

查看更多
等我变得足够好
7楼-- · 2019-01-09 23:14

Another answer would be the NodeJS!

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Using terminal you will be able to start it using node command.

$ node
> 2 + 4
6
> 

You can also run a JavaScript file like this:

node file.js

« Install it NOW »

查看更多
登录 后发表回答