How do you run JavaScript script through the Terminal?

前端 未结 16 1762
离开以前
离开以前 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:08

    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
    > 
    

    Note: If you want to exit just type

    .exit
    

    You can also run a JavaScript file like this:

    node file.js
    

    « Install it NOW »

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

    If you are on a Windows PC, you can use WScript.exe or CScript.exe

    Just keep in mind that you are not in a browser environment, so stuff like document.write or anything that relies on the window object will not work, like window.alert. Instead, you can call WScript.Echo to output stuff to the prompt.

    http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx

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

    It is crude, but you can open up the Javascript console in Chrome (Ctrl+Shift+J) and paste the text contents of the *.js file and hit Enter.

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

    If you're using MacBook.

    1. Set up node.js in your system and open up the terminal
    2. Navigate to the directory, where the js file is saved.
    3. To execute run node <filename.js>

    example, if filename is script.js run node script.js

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

    You can use a shebang file :

    script.js

    #!/usr/bin/env node
    console.log('Hello terminal');
    

    And run it

    ./script.js
    

    Don't forget to chmod +x script.js

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

    If you have a Mac you can get jsc a javascript console in OS X (Terminal) by typing

    /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc
    

    in Terminal.app.

    You could also run one of your .js script by adding its name as an argument for jsc, like this:

    jsc your_awesome_script_name.js
    

    Notice: I use console.log() during development but jsc needs the debug() function instead.

    On Ubuntu you have some nice ECMAScript shells at your disposal. Between them it's worth to mention SpiderMonkey. You can add It by sudo apt-get install spidermonkey

    On Windows as other people said you can rely on cscript and wscript directly built on the OS.

    I would add also another :) way of thinking to the problem, if you have time and like to learn new things i'd like to mention coffee-script that has its own compiler/console and gives you super-correct Javascript out. You can try it also on your browser (link "try coffeescript").

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