Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 21, 2025

This PR fixes a ReferenceError: __dirname is not defined that occurs when running the plugin in Node.js environments that use ESM modules.

Problem

The plugin was using the CommonJS __dirname variable in the runNcu method:

path.resolve(__dirname, '../node_modules/npm-check-updates/build/src/bin/cli.js')

However, since the project is configured as an ESM module ("type": "module" in package.json), __dirname is not available and causes a runtime error:

[13/02/2025, 17:01:20] [Plugin Update] ReferenceError: __dirname is not defined
    at PluginUpdatePlatform.runNcu (file:///var/lib/homebridge/node_modules/@homebridge-plugins/homebridge-plugin-update-check/src/index.ts:71:20)

Solution

Replaced the CommonJS __dirname with the ESM equivalent:

// Before
path.resolve(__dirname, '../node_modules/npm-check-updates/build/src/bin/cli.js')

// After  
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../node_modules/npm-check-updates/build/src/bin/cli.js')

This change:

  • Adds the necessary import { fileURLToPath } from 'node:url' import
  • Uses fileURLToPath(import.meta.url) to get the current module's file path
  • Uses path.dirname() to extract the directory path

The fix maintains the same functionality while being compatible with ESM modules.

Testing

  • ✅ TypeScript compilation succeeds
  • ✅ ESLint passes without issues
  • ✅ Unit tests pass
  • ✅ No remaining __dirname references in the codebase

Fixes #85.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Aug 21, 2025

@donavanbecker 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: donavanbecker <9875439+donavanbecker@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] ReferenceError Fix ReferenceError: __dirname is not defined in ESM module Aug 21, 2025
@Copilot Copilot AI requested a review from donavanbecker August 21, 2025 00:14
Copilot finished work on behalf of donavanbecker August 21, 2025 00:14
@donavanbecker
Copy link
Contributor

@justjam2013 thoughts?

@donavanbecker donavanbecker marked this pull request as ready for review August 21, 2025 00:21
@donavanbecker donavanbecker changed the base branch from latest to beta-2.3.1 August 21, 2025 00:45
@donavanbecker donavanbecker merged commit 5668b01 into beta-2.3.1 Aug 21, 2025
14 checks passed
@donavanbecker donavanbecker deleted the copilot/fix-85 branch August 21, 2025 00:45
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.

ReferenceError
2 participants