LoginSignup
10
3

More than 3 years have passed since last update.

error TS1192: Module '"fs"' has no default export.

Last updated at Posted at 2020-07-21

モジュール '"fs"' に既定エクスポートがありません
error TS1192: Module '"fs"' has no default export.

のようなエラーが出た時の対処法メモ

はじめに

これはnode.jsのデフォルトモジュールのfsが原因で起こるエラーでは有りません。
TypeScriptの仕様によるものです。
tsconfig.jsonのオプションでこのエラーを解決する方法があります。

結論

tsconfig.json"allowSyntheticDefaultImports": trueを追記する

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true
  }
}

または

// error TS1192: Module '"fs"' has no default export.
import fs from 'fs'

// no error
import * as fs from 'fs'

解説(tsconfigのオプション追加は省略)

TypeScriptでES6のデフォルト構文を使うとエラーでコンパイルエラーが起きます。

そういう時は下記の様な構文で指定ファイルの全てをインポートしてあげましょう。

import * as fs from 'fs'

エラーが出ないもの

また、下記の様に個別にexportされたものをimportする場合はエラーが出ません。

// hogeModuleファイル
export const hoge = () => {}
import { hoge } from 'hogeModule'

上記と同様にexport defaultが指定されているものもエラーになりません。

// hogeModule
export default class hoge {}
import hoge from 'hogeModule'

記事のもと

https://github.com/microsoft/TypeScript/issues/3337
https://qiita.com/bouzuya/items/edf5274241b50f32c621
https://qiita.com/alfas/items/539ade65926deb530e0e

10
3
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
10
3