eslint-plugin-import: Possible readme error for eslint-import-resolver-node
Hi there. I’m currently attempting to get this plugin working with (the equivalent of) a custom NODE_PATH. Was looking at the eslint-import-resolver-node documentation, which says to add an import/resolver
key to the .eslintrc
file, but after checking the code I see that it actually looks for import/resolve
(in lib/core/resolve.js
).
I’m not sure if I’m missing something, or if there’s a discrepancy.
Relevant versions:
- eslint@1.8.0
- eslint-plugin-import@0.8.1
- eslint-import-resolver-node@0.1.0
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 20 (8 by maintainers)
Just to follow up, I was able to get it to work this way, using this .eslintrc with the following options:
It seems that the contents of
import/resolve
is passed on directly to resolve, so it’s not wrapped inside anode
key like in the readme.It’s very striking how this thing just keeps coming up time and again, though. Every once in a while someone mentions NODE_PATH and the need to do imports relative to the current module, and every time it’s the same story: best practices, small modules, don’t mess with Node’s way of doing things.
Regardless of what any one of us might think, to me this does not indicate simply a failure to communicate how Node works, but a disagreement among a significant amount of Node users on how it should work in the first place. The numerous iffy workarounds are testament to this. I don’t think it’s correct to say it’s “widely considered a bad practice” because this issue just refuses to die.
In my view, the core problem is that the Node module resolution algorithm doesn’t support import paths relative to the current package—only to the current file, to the filesystem root, or to any file (not even package, necessarily) inside
node_modules
. If it were possible for a file in packagemyPackage
to importmyPackage/components/file.js
, this whole discussion could probably be avoided. That’s how Python works and probably loads of other languages. It’s very strange that you can have import statements that start with any package name, except your own package, and I’m curious why it was designed with this obvious limitation to begin with.We’re setting a local path to our project js files. Relative paths are just too error prone and frustrating (“ugh, forgot/added an extra
../
again!”). We put it one directory up so that we can prefix and make it more clear what is coming from our project vs what comes from externalnode_modules
, soimport * from 'js/responses/action_creators'
vsimport React from 'react'
.NODE_PATH
is not actually deprecated and it’s still officially supported in their docs, as of version 7.8.0, though admittedly they do discourage it. Many tools these days take some sort of configuration that allow you to not mess withNODE_PATH
, however:Jest takes the configuration
modulePaths
:Webpack takes the configuration
resolve.modules
:Mocha doesn’t have a way out of the box, as far as I can tell, but I was able to work around it using the
app-module-path
npm package in our mocha helper file:I’m not aware of a way to get around it using browserify, so you may be stuck with modifying
NODE_PATH
.And to make
eslint-plugin-import
work correctly, I added the following to my.eslintrc.yml
: