diff --git a/CHANGELOG.md b/CHANGELOG.md index 29f0fd32f..66f2a75b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## [Unreleased] +### Fixed +- [`no-duplicates`]: do not unconditionally require `typescript` ([#2665]) + ## [2.27.1] - 2023-01-11 ### Fixed @@ -1361,6 +1364,7 @@ for info on changes for earlier releases. [#211]: https://github.com/import-js/eslint-plugin-import/pull/211 [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 [#157]: https://github.com/import-js/eslint-plugin-import/pull/157 +[#2665]: https://github.com/import-js/eslint-plugin-import/issues/2665 [#2444]: https://github.com/import-js/eslint-plugin-import/issues/2444 [#2412]: https://github.com/import-js/eslint-plugin-import/issues/2412 [#2392]: https://github.com/import-js/eslint-plugin-import/issues/2392 diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index b896f442a..93ec36a8e 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -1,7 +1,11 @@ import resolve from 'eslint-module-utils/resolve'; import docsUrl from '../docsUrl'; import semver from 'semver'; -import typescriptPkg from 'typescript/package.json'; + +let typescriptPkg; +try { + typescriptPkg = require('typescript/package.json'); +} catch (e) { /**/ } function checkImports(imported, context) { for (const [module, nodes] of imported.entries()) { @@ -114,7 +118,7 @@ function getFix(first, rest, sourceCode, context) { const preferInline = context.options[0] && context.options[0]['prefer-inline']; // a user might set prefer-inline but not have a supporting TypeScript version. Flow does not support inline types so this should fail in that case as well. - if (preferInline && !semver.satisfies(typescriptPkg.version, '>= 4.5')) { + if (preferInline && (!typescriptPkg || !semver.satisfies(typescriptPkg.version, '>= 4.5'))) { throw new Error('Your version of TypeScript does not support inline type imports.'); }