How to resolve "Undeclared variable or function 'module'" error thrown by js2-mode in Spacemacs

If you've setup Tern for Spacemacs, and you're seeing the following error:

Undeclared variable or function 'module'

add the following line to the user-init section of your .spacemacs file:

(setq js2-include-node-externs t)

For example, here's what my user-init section looks like:

shows user-init section from Spacemacs configuration

What does this line do?

In your js2-mode.el file (see .emacs.d/elpa/js2-mode/), there's a section that looks like this:

(defvar js2-node-externs
  (mapcar 'symbol-name
          '(__dirname __filename Buffer clearInterval clearTimeout require
            console exports global module process setInterval setTimeout
            querystring setImmediate clearImmediate))
  "Node.js externs.
Set `js2-include-node-externs' to t to include them.")

Setting this variable helps define module for js2-mode.

Thanks to this answer on Stack Exchange for helping me figure this out.

Comments

Leave a comment

What color are brown eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.