2
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?

モジュール ‘./utils/calcTotalTime.js’ の宣言ファイルが見つかりませんでした エラー

2
Posted at

はじめに

モジュール ‘./utils/calcTotalTime.js’ の宣言ファイルが見つかりませんでした のエラーについてまとめました。

問題

ReactとTypeScriptの環境で、jsファイルでUtilを作成し、tsxファイルにインポートしたら表題のエラーが発生しました。

// ~/<ProjectName>/src/App.tsx

import { calcTotalTime } from './utils/calcTotalTime.js';

export const App = () => {
  const totalTime = calcTotalTime(records);  // モジュール ‘./utils/calcTotalTime.js’ の宣言ファイルが見つかりませんでした。

  // ...
};
// ~/<ProjectName>/src/utils/calcTotalTime.js

export function calcTotalTime (records) {
  const totalTime = records
    .map((record) => record.time)
    .reduce((accumulator, currentValue) => accumulator + currentValue, 0);

  return totalTime;
}

解決方法

tsxファイルではデフォルトで jsファイルをインポートできないため、tsconfig.app.json の compilerOptions で “allowJs”: true のオプションを追加することで解決できました。

// ~/<ProjectName>/tsconfig.app.json

{
  "compilerOptions": {
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "erasableSyntaxOnly": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedSideEffectImports": true,

    "allowJs": true // ← 追加したオプション
  },
  "include": ["src"]
}

おわりに

tsconfig.json の役割や何ができるかも学習していきたいと思います。

参考

2
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
2
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?