2
1

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でts-nodeを使用して、実行するとCustomError: Cannot find moduleと出る。

2
Posted at

はじめに

npx ts-node --esm -r dotenv/config src/batch/index.ts 

上記のコマンドで下の index.ts の実行結果を確認しようとすると
モジュールがimportできないとエラーが出ました。

index.ts
import { supabase } from "../api/supabase/supabaseClient";

async function deleteUserAndSkill() {
    // 前日のユーザ情報を削除する処理
}

deleteUserAndSkill()
  .then(() => console.log("Delete Completed!!"))
  .catch((error) => console.error(error));

解決方法

下記のように「.js」をつけてimportしてあげる必要があります。

import { supabase } from "../api/supabase/supabaseClient.js";

ESModule形式では、ファイル拡張子を完全に指定してあげる必要があります。
typescriptは最終的にjavascritptに変換されて実行されるため ➡「.js」

前提:
toconfig-pathsのインストールが必要です

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?