eslint-import-resolver-alias
eslint-import-resolver-alias copied to clipboard
Throw errors when resolving packages whose "main" is set to a folder (i.e enzyme)
When using eslint-import-resolver-alias with enzyme, it throws an error.
// item.test.tsx
import { shallow } from 'enzyme';
// .eslint.json
"settings": {
"import/resolver": {
"alias": {
"map": [["src", "./src"]],
"extensions": [".ts", ".tsx"]
}
}
}
1:1 error Resolve error: Cannot find module '/Users/zeroliu/Developer/Advanced-React/sick-fits/frontend/node_modules/enzyme/build'. Please verify that the package.json has a valid "main" entry
at tryPackage (internal/modules/cjs/loader.js:323:19)
at Function.Module._findPath (internal/modules/cjs/loader.js:680:18)
at findModulePath (/Users/zeroliu/Developer/Advanced-React/sick-fits/frontend/node_modules/eslint-import-resolver-alias/index.js:100:27)
It works for other packages. The error goes away if I stop importing enzyme or remove eslint-import-resolver-alias from my pipeline. Looking at enzyme package.json, its "main" property is set to "build" folder, which is different from most NPM packages that use a js file as entry. According to the call stack, I believe the problem is caused by calling the private __findPath
method directly.
const filename = Module._findPath(request, paths);
I'm not quite familiar with the internal implementation of node module. Please help.
For now, there are these workarounds:
- map enzyme to a random path.
"map": [["src", "./src"], ["enzyme", "foo"]]
- update enzyme package.json main to "build/index.js"
Option 1 is probably better since we don't want to touch third party packages.
I forked this project and added a quite hacky code to ignore this "main" problem, you might check this out
https://github.com/ming436534/eslint-import-resolver-alias