網站搜尋

typescript - Eslint 'rxjs/operators' 應該被列在專案的依賴中


所以我在我的檔案中有這個導入

import { catchError, finalize } from 'rxjs/operators';

當我運行 eslint 時,我得到這個錯誤:

 error  'rxjs/operators' should be listed in the project's dependencies. Run 'npm i -S rxjs/operators' to add it  import/no-extraneous-dependencies

我在 package.json 的 dependencies 列表中有 rxjs,但 eslint 似乎認為 'rxjs/operators' 是另一個套件。

在我的 eslintrc 文件中,我有這條規則:

"import/no-extraneous-dependencies": [
        "error",
        {
            "devDependencies": ["**/*.test.ts", "**/*.test.tsx"]
        }
    ],

答案#1

我遇到了同樣的問題,並通過添加以下內容來解決它

...
"settings": {
   "import/core-modules": ["rxjs/operators"]
},
...

在我的 .eslintrc.json 配置文件中。

答案#2

@Wana 的解決方案有效,但你可能會發現這實際上是 eslint-plugin-import 規則的問題。

如果你將 eslint-plugin-import 更新到 v2.23.4,你可能會發現這個問題已經修復。套件解析算法最近有一個回歸問題。

npm update eslint-plugin-import

閱讀更多關於這個問題的資訊