37

I got this when I was trying to find some way to run my JavaScript programs through terminal. The run and load command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store locally on my system.

EDIT: I am trying to solve Project Euler Q10 in JavaScript. So this is the program that I want to run in NodeJs or JSC. I need help in running the JavaScript files in Node and JSC. Any example will be really helpful.

Thank You all.

SocialCircus
  • 2,110
  • 6
  • 24
  • 35
  • The instructions you link to seem clear enough. What problem are you having? What are you trying? How does the behaviour you get differ from the behaviour you expect? – Quentin Jul 26 '11 at 12:48

4 Answers4

81

Complements of this post, JSC lives at

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

and is not in the shell PATH by default. You can fix that with

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

jsc takes filenames as arguments. You could run a file named demo.js with

jsc demo.js

Note that you'll have to use debug() instead of the conventional console.log() in your script to see any output.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
12

NOTE for MacOS Catalina (10.15.x) users

jsc now lives on a new path:

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

Make that adjustment to @hurrymaplelad's instructions and you'll be good to go.

sherb
  • 5,845
  • 5
  • 35
  • 45
  • `ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc /usr/local/bin` and then from my home dir: `jsc -h` and I get `-bash: jsc: command not found` – mplungjan Aug 28 '21 at 16:35
0

You're not very clear on what sort of programs these scripts are. Do they do local tasks (e.g. interact with the local file system)? Or are they web scripts that are supposed to interact with (HT|X)ML documents?

If your usage scenario is the former, I recommend using node.js. It is under heavy development, but it is already very usable. For my first project with Node I wrote an XMPP chat bot, completely in JavaScript.

Edit:

I seem to have overlooked "using jsc" in the question and answered "How to run JavaScript on OS X?" instead. Still, I think Node is a better alternative if the author is looking to use JavaScript in place of something like Python or Perl.

James Sumners
  • 14,485
  • 10
  • 59
  • 77
-1

For future reference: I made a GitHub gist with updated info on using JSC, is available here.