How do you run JavaScript script through the Terminal?

前端 未结 16 1764
离开以前
离开以前 2020-12-02 04:00

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

相关标签:
16条回答
  • 2020-12-02 04:20

    You can also use phantomjs Download phantomjs depending on the system (my case is Max OSX) from phantomjs.org .You should put the path to phantomjs installation folder on the top of your javascript file. eg. #!./bin/phantomjs Save your code. Go to the terminal where your javascript is saved and you can run using > phantomjs filename.js

    0 讨论(0)
  • 2020-12-02 04:21

    Use node.js for that, here is example how to install node by using brew on mac:

    brew update && install node
    

    Then run your program by typing node filename.js, and you can use console.log() for output.

    0 讨论(0)
  • 2020-12-02 04:22

    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.

    0 讨论(0)
  • 2020-12-02 04:23

    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

    0 讨论(0)
  • 2020-12-02 04:25

    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.

    0 讨论(0)
  • 2020-12-02 04:27

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

    0 讨论(0)
提交回复
热议问题