diff --git a/CHANGELOG.md b/CHANGELOG.md index c155f9d38..74412fff4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`no-cycle`]: ignore imports where imported file only imports types of importing file ([#2083], thanks [@cherryblossom000]) - [`no-cycle`]: fix false negative when file imports a type after importing a value in Flow ([#2083], thanks [@cherryblossom000]) - [`order`]: restore default behavior unless `type` is in groups ([#2087], thanks [@grit96]) +- [`no-import-module-exports`]: Don't crash if packages have no entrypoint ([#2099], thanks [@eps1lon]) ### Changed - [Docs] Add `no-relative-packages` to list of to the list of rules ([#2075], thanks [@arvigeus]) @@ -794,6 +795,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099 [#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090 [#2087]: https://github.com/benmosher/eslint-plugin-import/pull/2087 [#2083]: https://github.com/benmosher/eslint-plugin-import/pull/2083 @@ -1262,6 +1264,7 @@ for info on changes for earlier releases. [@eelyafi]: https://github.com/eelyafi [@Ephem]: https://github.com/Ephem [@ephys]: https://github.com/ephys +[@eps1lon]: https://github.com/eps1lon [@ernestostifano]: https://github.com/ernestostifano [@fa93hws]: https://github.com/fa93hws [@fengkfengk]: https://github.com/fengkfengk @@ -1402,4 +1405,4 @@ for info on changes for earlier releases. [@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg [@xpl]: https://github.com/xpl [@yordis]: https://github.com/yordis -[@zloirock]: https://github.com/zloirock +[@zloirock]: https://github.com/zloirock \ No newline at end of file diff --git a/src/rules/no-import-module-exports.js b/src/rules/no-import-module-exports.js index 7ac56da39..8ce5f4c9a 100644 --- a/src/rules/no-import-module-exports.js +++ b/src/rules/no-import-module-exports.js @@ -4,7 +4,13 @@ import pkgUp from 'pkg-up'; function getEntryPoint(context) { const pkgPath = pkgUp.sync(context.getFilename()); - return require.resolve(path.dirname(pkgPath)); + try { + return require.resolve(path.dirname(pkgPath)); + } catch (error) { + // Assume the package has no entrypoint (e.g. CLI packages) + // in which case require.resolve would throw. + return null; + } } module.exports = { diff --git a/tests/files/missing-entrypoint/package.json b/tests/files/missing-entrypoint/package.json new file mode 100644 index 000000000..4138a88f4 --- /dev/null +++ b/tests/files/missing-entrypoint/package.json @@ -0,0 +1,3 @@ +{ + "bin": "./cli.js" +} diff --git a/tests/src/rules/no-import-module-exports.js b/tests/src/rules/no-import-module-exports.js index bd18bf477..9ffbe6b56 100644 --- a/tests/src/rules/no-import-module-exports.js +++ b/tests/src/rules/no-import-module-exports.js @@ -57,6 +57,13 @@ ruleTester.run('no-import-module-exports', rule, { filename: path.join(process.cwd(), 'tests/files/some/other/entry-point.js'), options: [{ exceptions: ['**/*/other/entry-point.js'] }], }), + test({ + code: ` + import * as process from 'process'; + console.log(process.env); + `, + filename: path.join(process.cwd(), 'tests/files/missing-entrypoint/cli.js'), + }), ], invalid: [ test({