1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【typescript-eslint】A `require()` style import is forbidden.

Posted at

はじめに

Jestの環境構築中、手順通りに記載したところ掲題のエラーになったため、備忘録として残します。

問題

A require() style import is forbidden.エラーが発生する

jest.setup.ts
import "@testing-library/jest-dom";

require("dotenv").config();

image.png

本来は、require("dotenv").config();を記述することで、プロジェクト直下に配置した.envファイルを読み込み、process.envに設定してくれる。

require("dotenv") : dotenvパッケージをimportする
.config() : .envファイル内に定義している環境変数を読み込み、process.envに設定

解決方法

import形式に修正した

TypeScriptを使用する場合、requireを使うよりもimportを使うことが推奨されている
ES-lintの@typescript-eslint/no-require-importsルールに違反していたため、エラーが発生していた。

jest.setup.ts
import "@testing-library/jest-dom";
+ import { config } from "dotenv";

- require("dotenv").config();
+ config();

おわりに

require()でもパッケージのインポートが可能なことを学びました。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?