Skip to content

fix: use pathToFileURL for dynamic import on Windows#12

Open
wan-kong wants to merge 2 commits into
lonewolfyx:masterfrom
wan-kong:fix/esm-in-windows-path
Open

fix: use pathToFileURL for dynamic import on Windows#12
wan-kong wants to merge 2 commits into
lonewolfyx:masterfrom
wan-kong:fix/esm-in-windows-path

Conversation

@wan-kong
Copy link
Copy Markdown

@wan-kong wan-kong commented May 14, 2026

  1. 使用 rimraf 替代rm命令,保证可以在windows环境中正常build
  2. 修复 Windows 上运行 npx usage-board@latest 时报 ERR_UNSUPPORTED_ESM_URL_SCHEME 错误的问题。

根因: src/index.tsloadNitroEntrypoint 函数使用 resolve() 获取绝对路径后直接传给 import()。在 Windows 上 resolve() 返回 C:\Users\...\server\index.mjs 形式的路径,但 Node.js ESM loader 要求动态 import() 必须使用 file:// URL scheme。

修复: 使用 Node.js 内置的 pathToFileURL() 将绝对路径转为 file:// URL,兼容所有平台。

- const mod = await import(entryPath)
+ const mod = await import(pathToFileURL(entryPath).href)
  • Linux/macOS 上 pathToFileURL 产生 file:///path/to/file,行为不变
  • Windows 上 pathToFileURL 产生 file:///C:/Users/.../file,ESM loader 可正确加载

Work with https://app.devin.ai/

Summary by CodeRabbit

  • Chores
    • Enhanced build script with cross-platform file removal improvements for more reliable builds across operating systems.
    • Optimized server module loading to better support dynamic ESM imports and resolution at runtime.

Review Change Stack

devin-ai-integration Bot and others added 2 commits May 14, 2026 07:29
On Windows, resolve() returns paths like C:\Users\...\server\index.mjs,
but Node.js ESM loader requires file:// URLs for dynamic imports.
Using pathToFileURL(entryPath).href converts the absolute path to a
proper file URL, fixing ERR_UNSUPPORTED_ESM_URL_SCHEME on Windows.

Co-Authored-By: Wankong  <[email protected]>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 90b6c076-8781-4c6b-b70c-34e083eca30b

📥 Commits

Reviewing files that changed from the base of the PR and between e09a909 and 4934f1c.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (2)
  • package.json
  • src/index.ts

📝 Walkthrough

Walkthrough

The PR updates the build script to use rimraf instead of rm for cross-platform file removal compatibility, and refactors the dynamic ESM module import to convert filesystem paths to file URLs before importing, improving module resolution reliability.

Changes

Build and Module Loading Improvements

Layer / File(s) Summary
Build script and rimraf dependency
package.json
Build script replaces rm with rimraf for removing dist/nitro.json, and rimraf ^6.1.3 is added to devDependencies for cross-platform compatibility.
Dynamic import with file URL
src/index.ts
pathToFileURL is imported from node:url and applied to the entrypoint path before dynamic import to ensure proper ESM module resolution.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A path becomes a URL, rimraf dances cross-platform free,
Modules load with grace, no slashes to flee—
Small tweaks, big wins, the code runs clean and true,
Build scripts so portable, Windows loves it too! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: using pathToFileURL for dynamic imports to fix Windows compatibility issues.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant