This question Node.js prompt '>' can not show in eshell solve the problem for the node repl, but that solution don't work when you call node from npm.
By example if I make
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
^[[1G^[[0Jname: (nodo1) ^[[15G
Or if you have a package.json with a "scripts" : { "start" : "node" }
$ npm start
npm WARN package.json nodo1@0.0.1 No README.md file found!
> node
^[[1G^[[0J> ^[[3G
I know this one could be solved using "start" : "env NODE_NO_READLINE=1 node", but write this everywhere isn't a find solution. And maybe other user of the package don't use emacs and need set the env var in other way.
I have try with a alias for npm setting NODE_NO_READLINE=1 but the same result
alias npm='env NODE_NO_READLINE=1 npm'
This is the comint mode filtering the special characters many shell front-ends use to colorize the text. You are probably familiar with messages like "using a dumb terminal or Emacs terminal" if you ever interacted with a shell program through Emacs. Some can detect that Emacs or dumb terminal is used and will not send characters which those can't interpret, but Node doesn't do it. Regardless, you could use something like this:
(add-to-list
'comint-preoutput-filter-functions
(lambda (output)
(replace-regexp-in-string "\\[[0-9]+[GK]" "" output)))
somewhere in your .emacs.
Stackoverflow won't copy the control characters properly, the very first character in the regexp (right after the quote and before the slash) is the ^[
character. You can input it in Emacs by doing C-q 0 3 3 anything you type next will cause the control character to be inserted in the current buffer.