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

if (!supabaseUrl) throw new Error('supabaseUrl is required.')でtsファイル実行できない

Posted at

はじめに

TypeScriptのファイル.tsを実行して、supabaseのテーブルデータを削除しようとしましたが、エラーで詰まってしまいました。
解決に時間がかかったので解決策をまとめます。

1つ目の事象

tsファイルを実行

$ npx ts-node --esm ./batch/index.ts

すると以下のエラーが発生しました。

/home/your_name/react-dev/name-card-app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366
    throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: Cannot find module '/home/your_name/react-dev/name-card-app/src/utils/supabase' imported from /home/your_name/react-dev/name-card-app/batch/index.ts

原因

supabaseのファイルが読み込めていませんでした。
tsファイルを単独で実行する際には、拡張子をつける必要があるみたいです。

解決策

import { supabase } from "../src/utils/supabase";
// 下に修正
import { supabase } from "../src/utils/supabase.ts";

tsconfig.jsonも拡張子が使えるように修正

tsconfig.json
{
  "compilerOptions": {
    "allowImportingTsExtensions": true, // 追加

2つ目の事象

このエラーの解決後に、再度実行すると、またエラーが発生しました。
supabaseUrlがありませんとエラーが出ています。

/home/your_name/react-dev/name-card-app/node_modules/@supabase/supabase-js/src/SupabaseClient.ts:75
    if (!supabaseUrl) throw new Error('supabaseUrl is required.')
                            ^
Error: supabaseUrl is required.
    at new SupabaseClient (/home/your_name/react-dev/name-card-app/node_modules/@supabase/supabase-js/src/SupabaseClient.ts:75:29)
    at createClient (/home/your_name/react-dev/name-card-app/node_modules/@supabase/supabase-js/src/index.ts:40:10)
    at file:///home/your_name/react-dev/name-card-app/src/utils/supabase.ts:4:25
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

原因

直接tsファイルを実行する際は、envが読み込まれないようです。

解決策

最初に環境変数を読み込んであげればOKです。

import 'dotenv/config';

まとめ

悩んだところなので、お役に立てば幸いです。

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