ternjs / tern_for_sublime

Sublime Text package adding Tern support
MIT License
801 stars 52 forks source link

NODE_PATH #118

Closed igl closed 4 years ago

igl commented 8 years ago

We went for setting the NODE_PATH to ./src to avoid ugly imports like import thing from '../../../../thing.js'

Is it possible to set a path for tern to lookup modules using ES6 imports? I can only find the baseDir setting for the requirejs plugin.

marijnh commented 8 years ago

If you're using the node_resolve plugin (or the node plugin that depends on it), you can probably just also set the variable when you start your Tern server, and have things work (it just calls through to node's built-in resolution logic).

natealcedo commented 7 years ago

How does this work? The documenation isnt clear about this

shawnaxsom commented 7 years ago

@ndaljr or anyone else trying to get this to work, I have it working in tern_for_vim, I am assuming tern_for_sublime is similar.

You have to run your editor OR Tern server with the environment variable. If using BASH, just prefix NODE_PATH="" before your command to launch your editor (or Tern server).

For Vim, assuming your source file directory is a "src" folder under the directory ".tern-project" is in:

$ NODE_PATH="src” vim

or

$ NODE_PATH="src" ~/.vim/bundle/tern_for_vim/node_modules/tern/bin/tern $ vim

I also needed node_resolve and es_modules plugins. I optionally added webpack configuration from create-react-app.

.tern-project (stored in the main project directory, usually where node_modules is found, just above ./src for me)

{
  "ecmaVersion": 6,
  "libs": [
    "browser",
    "react"
  ],
  "plugins": {
    "node_resolve": {},
    "es_modules": {},
    "webpack": {
      "configPath": "../node_modules/react-scripts/config/webpack.config.dev.js"
    }
  }
}
natealcedo commented 7 years ago

@axs221 I gave up trying to get this to work at the time. I eventually settled to doing something hacky by using symlinks. Works perfectly this way but you have to configure your npm scripts for it to work. This way it's not something specific for people using tern as well. I realize that by going down this path though that I immediately drop support for people using windows but it's an acceptable use case for my team since everyone is on a unix based system. Here is what the scripts sections of my package.json looks like.

However I will take a look at your suggestion of declaring the NODE_PATH value when I run my vim. Thanks for that suggestion! I didn't think about that. Like I said, the docs weren't clear about that.

 "scripts": {
    "build": "npm run clean && npm run cleansymlink && npm run symlinkproduction && babel src -s -d build",
    "clean": "rm -rf build/",
    "cleansymlink": "cd node_modules && rm app lib config.js || true",
    "dev": "  NODE_ENV=development nodemon --watch src --exec babel-node ./src/index.js ",
    "precommit": "lint-staged",
    "predev": "npm run cleansymlink && npm run symlinkdev",
    "pretest": "npm run cleansymlink && npm run symlinkdev",
    "start": "npm run build && NODE_ENV=production node ./build/index.js",
    "staging": "npm run build && pm2 start ./build/index.js --name oromico",
    "symlinkdev": "cd node_modules && ln -nsf ../src/app && ln -nsf ../src/config.js && ln -nsf ../src/lib",
    "symlinkproduction": "cd node_modules && ln -nsf ../build/app && ln -nsf ../build/config.js && ln -nsf ../build/lib",
    "test": "NODE_ENV=development mocha -w --recursive --compilers js:babel-core/register"
  },
conradob commented 6 years ago

I use vim/nvim, also with NODE_PATH in my projects and I was unable to use GoTo and `GoToDefinition' .

The @axs221 suggestion worked for me:

You have to run your editor OR Tern server with the environment variable. If using BASH, just prefix NODE_PATH="" before your command to launch your editor (or Tern server).