Code Monkey home page Code Monkey logo

nodejs-repl.el's Introduction

nodejs-repl.el

Run Node.js REPL in Emacs

Description

This program is derived from comint-mode and provides the following features.

  • Token completion, same as Node.js REPL
  • File name completion in string
  • Incremental history search
  • Sending JavaScript code to REPL

Usage

Put this file in your Emacs Lisp path (e.g. ~/.emacs.d/site-lisp) and add the following line to your .emacs:

(require 'nodejs-repl)

Type M-x nodejs-repl to run Node.js REPL. See also comint-mode to check key bindings.

You can define key bindings to send JavaScript code to REPL as follows:

(add-hook 'js-mode-hook #'nodejs-repl-minor-mode)

When a version manager such as nvm is used to run different versions of Node.js, it is often desirable to start the REPL of the version specified in the .nvmrc file per project. In such case, customize the nodejs-repl-command variable with a function symbol. That function should query nvm for the Node.js command to run. For example:

(require 'nodejs-repl)
(defun nvm-which ()
  (let* ((shell (concat (getenv "SHELL") " -l -c 'nvm which'"))
         (output (shell-command-to-string shell)))
    (cadr (split-string output "[\n]+" t))))
(setq nodejs-repl-command #'nvm-which)

The nvm-which function can be simpler, and perhaps can run faster, too, if using Bash:

(defun nvm-which ()
  (let ((output (shell-command-to-string "source ~/.nvm/nvm.sh; nvm which")))
    (cadr (split-string output "[\n]+" t))))

Author

Takeshi Arabiki (abicky)

Copyright and License

Copyright (C) 2012-2024 Takeshi Arabiki (abicky)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

nodejs-repl.el's People

Contributors

abicky avatar boehm-s avatar jiangbinzhao avatar jordonbiondo avatar nverno avatar puercopop avatar purcell avatar utkarsh181 avatar wailo avatar werelax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nodejs-repl.el's Issues

it doesn't support ES6

When write some code with ES6 features, the nodejs-repl can't work well.

> class Album{
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at Object.exports.createScript (vm.js:24:10)
    at REPLServer.defaultEval (repl.js:137:25)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:393:12)
    at emitOne (events.js:77:13)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:833:16)
> >   constructor(currentSales){

Kill ring contents are sometimes prepended to region before being sent to repl

Emacs:
version: 26.2

Nodejs-repl
Version: 20200114.1529
Commit: 8b90948

NodeJS:
Version: 13.8.0

Steps to reproduce:

  1. Start nodejs-repl
  2. Type `console.log("Hello world") into the repl
  3. Mark the undefined that the repl returns and copy it to the kill ring (M-w)
  4. Open a file tmp.js and enter console.log("Hello World") mark it, and then call nodejs-repl-send-region

The kill ring contents are prepended to what is sent to the nodejs-repl:

> console.log("Hello World")
Hello World
undefined
> undefined.editor
// Entering editor mode (^D to finish, ^C to cancel)
console.log("Hello World");
undefined
Hello World
undefined
> 

Process nodejs finished

nodejs-repl-send-last-sexp fails in certain cases

First of all thanks for this tool!

I am having a hard time figuring out how to use nodejs-repl-send-last-sexp, for example if I run it on the following:

[1,2,3].map(function(number) {
  return number * 2
})
// <-- from here

then this is evaluated

function(number) {
  return number * 2
}

while I would've liked the whole thing. Is there any way to have this? One could use nodejs-repl-send-region, as a fallback, is that intended?

I've had a look at the source code and the function responsible to figure out the extension of the sexp seems to be backward-sexp.

execute to minibuffer

First of all thank you for creating this package!

I've modified the nodejs-repl-execute function a bit to echo the results of the selected region in the minibuffer. This way a js file can be used as a scratch buffer:
js-scratch

Do you think this could be a useful addition to the package? Then I'll submit a PR for this.

More robust multi-line regions

I've had nodejs-repl-send-region fail when the region included a lambda definition. This won't run, but it might give you an idea:

supertest(options.shoreApi)
    .post('/Test/Enpoint')
    .set('Content-type', 'application/json')
    .send(testUser)
    .expect(status.OK)
    .end((err, res) => {
        if (err) {
            console.error(err);
        }
        console.log(res.body);
});

This would fail with the error message:

>     .post('/Test/Enpoint')
Invalid REPL keyword
>     .set('Content-type', 'application/json')
Invalid REPL keyword
>     .send(testUser)
Invalid REPL keyword
>     .expect(status.OK)
Invalid REPL keyword
>     .end((err, res) => {
Invalid REPL keyword
>         if (err) {
.....             console.error(err);
.....         }
ReferenceError: err is not defined
>         console.log(res.body);
ReferenceError: res is not defined
> });
});
^

SyntaxError: Unexpected token }

> 

Since NodeJS 6.4.0, it is possible to work around this by wrapping the multi-line string in a
".editor" multi-line-string Control-D (EOF) block.

I will send a PR that implement this.

Trying ts-node REPL

Hello! Thanks for this nice package first of all 😄

I know this is not really supported but it would be great to have typescript Repl. I am still considering if it is worth modifying this or having a completely new inf-typescript for instance. I think the sexp detection part can be difficult to achieve for typescript, and should be probably delegated to typescript.el.

Anyways, I am trying it out and I get:

⨯ Unable to compile TypeScript
[eval].ts (1,16): Property 'columns' does not exist on type 'WritableStream'. (2339)

Process nodejs exited abnormally with code 1

Checking the code a bit, it seems that the new process.stdout (I am using node 7.9.0) does not have columns anymore.
In general probably that block causes the problem and it is not required anymore form new repls. I might be wrong of course.

Would you be interestend in a defcustom that avoids passing the -e to the process?

In general I'd be interested in your opinion on an alternative Emacs mode/package.

Cannot open load file: nodejs-repl

Installing package and updating .emacs file gives this error on reload / load:

Node installed and working fine outside emacs. Have tried almost every possible solution to ger nodejs repl working on emacs, all throw errors after adding the require and logic in .emacs file.

Running on MacBookPro with Mavericks

comint-previous-input breaks repl input

M-x nodejs-repl. then enter an input:

> x = 1
1
>

Press M-p (bound to comint-previous-input) causes the buffer to look like this:

> x = 1
1
x = 1> 

It's then impossible (as far as I can see) to input more text. Pressing RET repeatedly gives this:

> x = 1
1
x = 1> x = 1> 
... 
... 
... 

Token completion appears to be broken in emacs 26.2

After updating to the latest nodejs-repl and emacs 26.2 tab completion in repl buffers hangs emacs.

Steps to reproduce are pretty straight forward.
Install emacs 26.2
Install latest nodejs-repl
M-x nodejs-repl
Type process. and hit tab, emacs freezes. ctr-g to unfreeze

Thanks!

How to remove warning message

Currently, I see a message in the repl saying:

.editor
// Entering editor mode (^D to finish, ^C to cancel)
console.log(1+4);
5
undefined
.>.

Q1. How do I get rid of the text "Entering editor mode..."?
Q2. Also, how do I change the prompt to just "> " instead of ".>."

Error in process filter

Emacs version: 27.1
nodejs-repl version: 0.2.4
Node version: 14.10.1

Steps to reproduce:
1. Open any .js file
2. Execute M-x nodejs-repl
3. digit any expression on the repl (e.g. 5 + 8)

I installed this mode and when i execute any expression in the REPL buffer i get the following errors:

error in process filter: run-hook-with-args: End of buffer
error in process filter: End of buffer

and the RELP hang up a little bit, i don't now much about elisp so i was wondering if someone could help me with this?

This is the config i use, nothing especial:

(use-package nodejs-repl
  :config
  (add-hook 'js-mode-hook
          (lambda ()
            (define-key js-mode-map (kbd "C-x C-e") 'nodejs-repl-send-last-expression)
            (define-key js-mode-map (kbd "C-c C-j") 'nodejs-repl-send-line)
            (define-key js-mode-map (kbd "C-c C-r") 'nodejs-repl-send-region)
            (define-key js-mode-map (kbd "C-c C-c") 'nodejs-repl-send-buffer)
            (define-key js-mode-map (kbd "C-c C-l") 'nodejs-repl-load-file)
            (define-key js-mode-map (kbd "C-c C-z") 'nodejs-repl-switch-to-repl))))

nvm-which fails if no .nvmrc present

Ifhtere's no nvm-which present, then the nvm-which funciton in the docs fails. I was able to fix it with:

  (defun nvm-which ()
    (let ((output (shell-command-to-string "source ~/.nvm/nvm.sh; nvm which")))
      (if (cl-search "Node Version" output)
          (setq output (shell-command-to-string "source ~/.nvm/nvm.sh; nvm which current"))
      (setq output (cadr (split-string output "[\n]+" t))))
    (message output)
    output))

autocompletion causes repl to hang

Emacs 26.3
nodejs-repl v0.2.2
Nodejs version v13.5.0

To reproduce, type: "Obj" and press TAB, which causes emacs to hang.
Pressing Ctrl-g unfreezes emacs.

Also, another interesting thing occurs when pressing Enter after this, the incorrect string gets sent to the repl.

> Obj
Uncaught ReferenceError: ObjectObj is not defined
    at repl:1
    at Script.runInContext (vm.js:131:20)
    at REPLServer.defaultEval (repl.js:425:29)
    at bound (domain.js:427:14)
    at REPLServer.runBound [as eval] (domain.js:440:12)
    at REPLServer.onLine (repl.js:750:10)
    at REPLServer.emit (events.js:305:20)

Might be related to #24 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.