hjdhjd/unifi-protect

Migration from 3 to 4 does not compile (ERR_REQUIRE_ESM)

sergicastellsague opened this issue · 2 comments

Describe The Problem:
After migrating from the version 3.0.4 to 4.8.0, the code does not compile.
This is the error thrown:

internal/modules/cjs/loader.js:1080
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

require() of ES modules is not supported.
require() of myproject/api/node_modules/unifi-protect/dist/index.js from myproject/api/src/index.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 myproject/api/node_modules/unifi-protect/dist/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from myproject/api/node_modules/unifi-protect/package.json.

To Reproduce:

  1. Create a new node app (node 14.16.0)
  2. in package.json: type:commonjs
  3. Tsconfig: target es6; module: commonjs
hjdhjd commented

This is an ESM only package as of 4.0 - it is an explicitly stated breaking change in the documentation. You cannot use it in a CJS project. Feel free to search around - there’s a lot of documentation out there about how to convert your project to ESM.

Best of luck.

This issue is locked to prevent necroposting on closed issues. Please create a new issue for related support requests, bug reports, or feature suggestions.