1 of 3
modulitos
  • 2.5k
  • 1
  • 22
  • 37

ac-js2-jump-to-definition not working in Nodejs

I am performing Nodejs development in emacs, but my environment seems incorrect. For example, when I run ac-js2-jump-to-definition, I get "no jump location found" in the minibuffer. How can I get this working?

Here is a sample of code, where the "jump-to-definition" function works in other environments like WebStorm:

In auth.js, I have this:

module.exports = function (options) {
  options = options || {}
  auth = {}
  
  auth.someFunction = function () {
    return "someFunction has been called"
  }
  
  return auth

}

Then I call auth from here:

var auth = require('auth')()
auth.someFunction()

But it does not work when I put the point over someFunction() above and run M-x ac-js2-jump-to-definition. Also, when I run js2-next-error, the require function in require('auth')() above is selected and gives the message "Undeclared variable or function 'require'".

I suspect that my Javascript environment is correct, but my Nodejs environment is not set up. What is wrong with my node environment, and how can I fix it?

Here is my relevant configuration from my init script:

;; JAVASCRIPT-MODE

;; js2-mode provides 4 level of syntax highlighting. They are * 0 or a negative value means none. * 1 adds basic syntax highlighting. * 2 adds highlighting of some Ecma built-in properties. * 3 adds highlighting of many Ecma built-in functions.
(setq js2-highlight-level 3)

(add-hook 'js2-mode-hook
          (lambda ()

            ;; allow window resizing via M-l and M-h
            (local-unset-key (kbd "M-l"))
            (local-unset-key (kbd "M-h"))
            (local-unset-key (kbd "M-j"))

            (local-set-key (kbd "C-j") 'ac-js2-jump-to-definition)
            (local-set-key (kbd "C-c C-n") 'js2-next-error)
            (define-key js2-mode-map (kbd "C-j") 'ac-js2-jump-to-definition)
            (linum-mode)
            (js2-reparse t)
            (ac-js2-mode)))

(add-to-list 'interpreter-mode-alist '("node" . js2-mode))

(add-to-list 'auto-mode-alist
      '("\\.js$" . js2-mode))

(require 'js2-refactor)
(js2r-add-keybindings-with-prefix "C-c C-m")
modulitos
  • 2.5k
  • 1
  • 22
  • 37