7

I've got Node installed using NVM, and Emacs (v25.2.1) isn't able to find Node. When I run M-: (executable-find "node") I get back nil.

However, when I run M-: (executable-find "cat") I get "/bin/cat". I assume the difference is due to my use of NVM.

How do I get Emacs to find Node when I've got Node installed using NVM?

Incidental technical details:

My Emacs was installed using MacPorts (using the emacs-app package that installs to the Mac Applications folder for a GUI experience), and I've got the following lines in my ~/.bashrc:

export NVM_DIR="/Users/fred/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

My ~/.bash_profile contains source ~/.bashrc.

I'm not sure if this is relevant to the problem, but I should mention that when I open M-x eshell, I get the following when trying to run Node:

$ node
node: command not found

However, I am able to run Node through M-x shell.

I did have limited success installing the emacs package (as opposed to the emacs-app package) from MacPorts. Using the Emacs from the emacs package (which installs to /opt/local/bin/emacs), I was able to evaluate in the scratch buffer:

(executable-find "node")

And successfully got the path to node. However, this same lisp expression evaluated in the Emacs from the emacs-app package yielded nil. This doesn't help me much, because I want to use the emacs from emacs-app, because it's the Emacs with the graphical interface for Mac, as I understand it.

I want to continue using NVM, please.

Evgeny Mikhaylov
  • 179
  • 1
  • 14
user2245766
  • 281
  • 2
  • 6
  • 1
    The doc-string for `executable-find` states: "*Search for COMMAND in ‘exec-path’ and return the absolute file name. Return nil if COMMAND is not found anywhere in ‘exec-path’.*" You can inspect the `exec-path` with `C-h v` or `M-x describe-variable`. If the path containing the `node` executable is not there, then you will be one step closer to troubleshooting the issue. – lawlist Jul 15 '17 at 23:03
  • Most likely, your .bashrc code has no effect because there's no bash involved (i.e. you start Emacs from the GUI which has not executed a bash shell). – Stefan Jul 16 '17 at 01:18
  • I run my emacs in the terminal. And my emacs shell sees the system node, rather than the nvm node. Seems very silly. I guess nvm is doing something sneaky. rvm works fine. The nvm default is set to the node version I want. But nvm just doesn't play nice. – nroose Sep 27 '19 at 22:59

1 Answers1

11

Best solution:

Install exec-path-from-shell. From the documentation, after calling (package-initialize), add the following to your .emacs file:

(when (memq window-system '(mac ns x))
  (exec-path-from-shell-initialize))

Another solution:

Add the following to your .emacs file:

(setq exec-path (append exec-path '("~/.nvm/versions/node/v6.3.0/bin")))

Your version number in the string above may differ. I don't like this solution as much as the "best solution," because it must be manually updated to keep the emacs path synchronized with the system path.

user2245766
  • 281
  • 2
  • 6
  • After appending a new path to `exec-path` to point to an `nvm` path it still shows me `"/usr/bin/node"` when evaluating `(executable-find "node")`. Maybe I should consider deleting `node` from `/usr/bin/node`? – Evgeny Mikhaylov Oct 15 '22 at 21:28
  • @EvgenyMikhaylov You could try `(push "/path/to/node/bin" exec-path)` instead of `append` to make the new path you're adding take precedence over `/usr/bin`. – g-gundam Oct 16 '22 at 20:44
  • @g-gundam thank you! But I have already deleted files `node` and `nodejs` from `/usr/bin/`. I have no idea though whether it could leave some remnants. – Evgeny Mikhaylov Oct 20 '22 at 20:28
  • @EvgenyMikhaylov I'd be careful with that. If your package management system installed node there, it might become confused when it can't find it later. – g-gundam Oct 20 '22 at 20:31