简体   繁体   中英

How do I get a Javascript runtime interpreter installed on OS X?

With OS X Xcode installed one can run ruby code in terminal with ">ruby somefile.rb" command (or from inside TextWrangler with the run command). You can also run a ruby interpreter from in terminal and type code in single lines while the interpreter retains variable objects.

Want same possibilities for Javascript. What must I install (if anything) on OS X to have that functionality? Not looking for an IDE as such just an interpreter that will run in Terminal.app?

I assume this is an allowed question for Stackoverflow based on other allowed questions about IDEs for various languages like: What's a good IDE for Python on Mac OS X?

There is a Javascript interpreter in the JavaScriptCore framework that comes with OS X. The interpreter is called jsc and can be found at the following path:

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

There is a built-in function, quit() , which will exit interactive mode.

If you want to make it easier to use I suggest creating a symlink to a location in your path, eg:

sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc /usr/local/bin

This will put a symbolic link in /usr/local/bin .

Node.js . It's the V8 engine + libraries + REPL.

Although Node is usually used for web.network-related applications, at its core it's just a plain JS engine and can even be used for shell scripting.

You can install it from the installer , brew or just ./configure && make from a Node.js' tarball.

There's also Rhino .

v8. Its the javascript engine used in google chrome. You have to compile it for mac OS X, though. Theres a good tutorial here .

You have two options:

  1. Use console in your Browser: Browsers such as chrome,safari, firefox come buildtin with console which are cappable of running javascript. to open open console on chrome. Press CTRL + SHIFT + J

  2. Install Nodejs: As others have pointed out, try http://nodejs.org/ using which you can run javascript in terminal app similar to irb

On macOS Big Sur you can easily access you OS JavaScript interpreter by adding your local user binary folder. You can create a new symlink in /usr/local/bin with

% ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc /usr/local/bin/

Then you can access the OS's JavaScript interpreter by just typing

% jsc

Hint: Do not forget, that there is no console object, you can use debug(...) instead

On macOS 10.13 High Sierra the location for the jsc binary is:

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

Using @mttrb's answer simply replace the path if you're on High Sierra. Seems like this binary has moved around a lot during macOS versions, so you would have to look around under the /System/Library/Frameworks/JavaScriptCore.framework/... path to find the location of jsc .

About documentation of jsc

The JavaScriptCore Swift/Objective-C framework of is a part of the Apple JavaScript additions for mac apps but the Apple Docs do very little to support the jsc binary. However, the original webkit docs do more:

https://trac.webkit.org/wiki/JSC

Here you will find the functions mentioned in other answers here, like the debug() (replaces console.log() ), the quit() (that exits terminal interactive shell mode) and so on.

Other JavaScript shells

Here is a list of a couple of JS shells , many of which are cross-platform and can be installed using a single pre-compiled binary (such as JSDB):

https://reference.codeproject.com/javascript/shells

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM