Run a JavaScript interpreter in an inferior process window
The first release, js-comint 0.0.1, is hosted on sourceforge but it has not been updated for five years.
- Can select node.js versions using nvm.el
- Use node.js by default.
- When loading file, detect what API to be used automatically
- Based on js-comint hosted on http://js-comint-el.sourceforge.net/
Place the js-comint.el somewhere say “~/mylisp/”.
Insert below code to “~/.emacs.d/init.el”,
(add-to-list 'load-path "~/mylisp/")
(require 'js-comint)
Using ELPA
Insert below code to “~/.emacs.d/init.el”,
(require 'package)
(package-initialize)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/"))
(package-install 'js-comint)
(require 'js-comint)
Using Cask
Add js-comint to your Cask file:
(depends-on "js-comint")
On window, you may need below setup:
(setq js-comint-program-command "C:/Program Files/nodejs/node.exe")
After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter.
Please note the directory node_modules
is automatically searched and appended into environment variable `NODE_PATH’. So you can use third party javascript without setup. For example, after npm install --save moment
, run below command in js-comint shell,
require('moment')().format('YYYY-MM-DD');
You can M-x js-clear
before M-x js-send-buffer
to get clean output.
In order to get cleaner output when using NodeJS, I suggest add below setup into .emacs
,
(defun inferior-js-mode-hook-setup ()
(add-hook 'comint-output-filter-functions 'js-comint-process-output))
(add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t)
You can set the `js-comint-program-command’ string and the `js-comint-program-arguments’ list to the executable that runs the JS interpreter and the arguments to pass to it respectively.
E.g., the default is:
;; You can also customize `js-comint-drop-regexp' to filter output
(setq js-comint-program-command "node")
(setq js-comint-program-arguments '("--interactive"))
Note that in the example above, the version of node that is picked up will be the first found in `exec-path’.
But you could use Rhino or SpiderMonkey or whatever you want. E.g. to set up the Rhino JAR downloaded from https://github.com/mozilla/rhino, do
(setq js-comint-program-command "java")
(setq js-comint-program-arguments '("-jar" "/absolute/path/to/rhino/js.jar"))
If you have nvm, you can select the versions of node.js installed and run them. This is done thanks to nvm.el
. nvm.el
is optional. So you need manually install it.
To enable nvm support, run
(js-do-use-nvm)
The first time you start the JS interpreter with run-js, you will be asked to select a version of node.js. If you want to change version of node js, run (js-select-node-version)
.
You can add the following couple of lines to your .emacs/init file to take advantage of key bindings for sending things to the JavaScript REPL:
; Remap Elisp's eval-last-sexp (C-x C-e) to eval JavaScript
(define-key js-mode-map [remap eval-last-sexp] #'js-comint-send-last-sexp))
(define-key js-mode-map (kbd "C-c b") 'js-send-buffer))