How to setup Spacemacs for JavaScript auto-complete and go-to-definition across files

This post assumes you have already done these steps.

To have intelligent JavaScript support across files in Spacemacs, we need to let Tern know which libraries and files we're using.

Create a JSON file named .tern-project in your project's root directory:

{
    "libs" : [
        "browser",
        "jquery"
    ],
    "loadEagerly": [ "js/*.js", "*.js" ],
    "plugins" : {
        "commonjs" : {},
        "node" : {},
        "requirejs" : {},
        "node_resolve" : {}
    }
}

Above, we're telling Tern to:

  1. Expect usages of JavaScript objects defined in web browsers, as well as the jQuery library.
  2. Inspect .js files in the project's root directory and in the js/ directory.
  3. Add plugins for NodeJS, etc.

Parts of the .tern-project file above are from this helpful post.

Edit your .spacemacs

If you're using NodeJS, you'll want to setup js2-mode so it knows what module means in your JavaScript. You might get similar errors if you're using other plugins and haven't set certain variables in your .spacemacs file.

Where can I find more about configuring Tern?

In the Tern documentation.

What's the shortcut for go-to-definition?

SPACE m g g. See more shortcuts in the JavaScript layer documentation.

Comments

Leave a comment

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