NorthernMan54 / homebridge-tcc

Honeywell Total Connect Comfort support for Homebridge
62 stars 23 forks source link

Must use import to load ES Module #99

Closed wbond closed 3 years ago

wbond commented 3 years ago

My old Rasp Pi installation got messed up recently so I moved this awesome plugin over to a newer 20.04 server I have running.

After installing homebridge-tcc, I get the following error:

====================
ERROR LOADING PLUGIN homebridge-tcc:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js
require() of ES modules is not supported.
require() of /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js from /usr/lib/node_modules/homebridge-tcc/lib/tcc.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/usr/lib/node_modules/homebridge-tcc/lib/tcc.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
====================

Node.js Version: v14.16.0 NPM Version: v6.14.11

I don't normally work in Node, but I mucked with package.json and changed p-queue to ^6.6.2 and that seemed to fix it.

mcmillster commented 3 years ago

Same problem here as of yesterday! I'll give this recommendation a shot, but something seems to be broken on the newest update. But I've been running this for the last few months without issue until the newest version.

Update*** I'm still getting the same error after that change, thank you for the suggestion! I'll mess with some more code and see if I can get it working and report back.

mcmillster commented 3 years ago

Okay I have this resolved with MANY steps... if anyone else has this issue and my steps below don't make sense feel free to message me :)

  1. convert index.js to index.cjs FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js

  2. Replace import to require const EventEmitter = require('eventemitter3'); //import EventEmitter from 'eventemitter3'; const pTimeout = require('p-timeout'); const TimeoutError = require('p-timeout').TimeoutError; //import pTimeout, { TimeoutError } from 'p-timeout'; const PriorityQueue = require('./priority-queue.cjs'); ***change to .cjs, you will create this file in next steps //import PriorityQueue from './priority-queue.js';

  3. Change export of class Change "export default class" to class PQueue extends EventEmitter {

           ADD at bottom of page `module.exports = PQueue;`
  4. Change lower-bounds.js file

FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/lower-bound.js replace with code below

`// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound // Used to compute insertion index to keep queue sorted after insertion function lowerBound(array, value, comparator) { let first = 0; let count = array.length; while (count > 0) { const step = Math.trunc(count / 2); let it = first + step; if (comparator(array[it], value) <= 0) { first = ++it; count -= step + 1; } else { count = step; } } return first; }

module.exports = lowerBound;`

  1. Change tcc.js

FILE HERE: usr/local/lib/node_modules/homebridge-tcc/lib/tcc.js

`const {
  default: PQueue
} = require('p-queue');`

TO 

const PQueue = require('p-queue');

joeroks commented 3 years ago

Okay I have this resolved with MANY steps... if anyone else has this issue and my steps below don't make sense feel free to message me :)

  1. convert index.js to index.cjs FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js
  2. Replace import to require const EventEmitter = require('eventemitter3'); //import EventEmitter from 'eventemitter3'; const pTimeout = require('p-timeout'); const TimeoutError = require('p-timeout').TimeoutError; //import pTimeout, { TimeoutError } from 'p-timeout'; const PriorityQueue = require('./priority-queue.cjs'); ***change to .cjs, you will create this file in next steps //import PriorityQueue from './priority-queue.js';
  3. Change export of class Change "export default class" to class PQueue extends EventEmitter {
           ADD at bottom of page `module.exports = PQueue;`
  4. Change lower-bounds.js file

FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/lower-bound.js replace with code below

`// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound // Used to compute insertion index to keep queue sorted after insertion function lowerBound(array, value, comparator) { let first = 0; let count = array.length; while (count > 0) { const step = Math.trunc(count / 2); let it = first + step; if (comparator(array[it], value) <= 0) { first = ++it; count -= step + 1; } else { count = step; } } return first; }

module.exports = lowerBound;`

  1. Change tcc.js

FILE HERE: usr/local/lib/node_modules/homebridge-tcc/lib/tcc.js

`const {
  default: PQueue
} = require('p-queue');`

TO 

const PQueue = require('p-queue'); . mcmillster, you lost me at step 1. How do I convert index.js to index.cjs? Sorry for the ignorance!!!

kg6ebj commented 3 years ago

My old Rasp Pi installation got messed up recently so I moved this awesome plugin over to a newer 20.04 server I have running.

After installing homebridge-tcc, I get the following error:

====================
ERROR LOADING PLUGIN homebridge-tcc:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js
require() of ES modules is not supported.
require() of /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js from /usr/lib/node_modules/homebridge-tcc/lib/tcc.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/lib/node_modules/homebridge-tcc/node_modules/p-queue/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/usr/lib/node_modules/homebridge-tcc/lib/tcc.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
====================

Node.js Version: v14.16.0 NPM Version: v6.14.11

I don't normally work in Node, but I mucked with package.json and changed p-queue to ^6.6.2 and that seemed to fix it.

quick and dirty, since I don't use nodeJS at all.

what you meant to say was install ncu then go to the Homebridge-tcc folder and run ncu it will say allot of the admins are out of date. edit package.json search for p-queue and change the version to ^6.6.2 to force that revision. then run npm install to force the install of that version of the p-queue plugin.

only error I received after that list of commands was an error about moment not being found, I reverted the moment plugin back to the one originally listed and it works flawlessly.

on a linux/bsd system the commands are cd to homebridge-tcc directory sudo npm install ncu sudo ncu (note the versions that it says to update) edit with your favorite editor package.json find the p-queue entry and replace >=x.x.x with ^6.6.2 sudo npm install then restart homebridge

mcmillster commented 3 years ago

Okay I have this resolved with MANY steps... if anyone else has this issue and my steps below don't make sense feel free to message me :)

  1. convert index.js to index.cjs FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/index.js
  2. Replace import to require const EventEmitter = require('eventemitter3'); //import EventEmitter from 'eventemitter3'; const pTimeout = require('p-timeout'); const TimeoutError = require('p-timeout').TimeoutError; //import pTimeout, { TimeoutError } from 'p-timeout'; const PriorityQueue = require('./priority-queue.cjs'); ***change to .cjs, you will create this file in next steps //import PriorityQueue from './priority-queue.js';
  3. Change export of class Change "export default class" to class PQueue extends EventEmitter {
           ADD at bottom of page `module.exports = PQueue;`
  4. Change lower-bounds.js file

FILE HERE: /usr/local/lib/node_modules/homebridge-tcc/node_modules/p-queue/dist/lower-bound.js replace with code below // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound // Used to compute insertion index to keep queue sorted after insertion function lowerBound(array, value, comparator) { let first = 0; let count = array.length; while (count > 0) { const step = Math.trunc(count / 2); let it = first + step; if (comparator(array[it], value) <= 0) { first = ++it; count -= step + 1; } else { count = step; } } return first; } module.exports = lowerBound;

  1. Change tcc.js

FILE HERE: usr/local/lib/node_modules/homebridge-tcc/lib/tcc.js

`const {
  default: PQueue
} = require('p-queue');`

TO 

const PQueue = require('p-queue'); . mcmillster, you lost me at step 1. How do I convert index.js to index.cjs? Sorry for the ignorance!!!

No worries! I wrote up what I did quickly not thinking it'd be of much help to anyone. You'll need to simply rename the file and change the file type to ".cjs". If needed, I can re-write the steps I took but it appears kg6ebj may have an easier fix to try than converting how everything is imported/exported.

joeroks commented 3 years ago

kg6ebj, your solution worked for me. I updated the p-queue version to ^6.6.2 and the module loads correctly...Thanks for the help.

rmsy commented 3 years ago

Confirming that updating p-queue to ^6.6.2 also worked for me. I just edited the package.json in the install directory (/usr/local/lib/node_modules/homebridge-tcc/ for me), and then re-ran npm install in that directory.

adrianguanipa commented 3 years ago

@NorthernMan54 Any chance we could get a new build that includes this fix? Please!

NorthernMan54 commented 3 years ago

A pull request with the fixes would be appreciated.

joeroks commented 3 years ago

Pull request issued. Hope I did it right as this is my first time doing a pull request...

NorthernMan54 commented 3 years ago

Tks, will take a look at this over the weekend

kamiezak commented 3 years ago

I can confirm @rmsy changes worked for me perfectly as well, thank you for the fix!

NorthernMan54 commented 3 years ago

Tks @joeroks for providing a fix for this issue, v0.2.22 should now resolve this.