Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

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?

tsxでError [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

Posted at

本記事について

tsxでTypeScriptを実行しようとして出たエラーがデバッグできず、ググっても対処法が出てこなかったので、同様の症状を経験する方のために記事に残そうと思いました。

結論

表題のエラーが出たらNode.jsをアップデートすると直るケースがある。

症状

package.json
{
  "name": "***",
  "type": "module",
  "scripts": {
    "dev": "tsx src/index.local.ts"
  },
  "devDependencies": {
  },
  "dependencies": {
  }
}

からの

npm run dev

で出たエラー


> dev
> tsx src/index.local.ts

node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
    at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
    at new NodeError (node:internal/errors:405:5)
    at throwIfUnsupportedURLScheme (node:internal/modules/esm/load:136:11)
    at defaultLoad (node:internal/modules/esm/load:87:3)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at load (file:///C:/Users/****/node_modules/tsx/dist/esm/index.mjs:2:1768)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:603:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

Node.js v18.18.0

対処経緯

node_modulesフォルダを削除;

del /s /q node_modules > NUL

からのWindowsエクスプローラーでフォルダを削除
からの

npm install
npm run dev

で症状再現。
筆者の環境ではnvmを使っているので、以下コマンドでNode.jsをアップデート

> nvm list

    20.9.0
  * 18.18.0 (Currently using 64-bit executable)
    16.16.0
    12.13.0
> nvm install 18
Downloading node.js version 18.20.4 (64-bit)... 
Extracting node and npm...
Complete
npm v10.7.0 installed successfully.


Installation complete. If you want to use this version, type

nvm use 18.20.4
> node --version
v18.18.0
> nvm use 18.20.4
Now using node v18.20.4 (64-bit)
> node --version
v18.20.4

これでnpm run devしたら意図したとおりにindex.local.tsを実行できた!!

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

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

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?