Unleash / unleash-proxy-client-js

A browser client that can be used together with the unleash-proxy.
Apache License 2.0
44 stars 46 forks source link

CommonJS imports are broken in v3.0.0 #173

Closed gamedevsam closed 8 months ago

gamedevsam commented 9 months ago

Describe the bug

My server suddenly stopped working during startup as a result of attempting to import unleash-proxy-client v3.0.0. I believe this is because unleash-proxy-client package defines "type": "module" in its package.json which is not something you should do in NPM packages (this field is for applications to define, not libraries, more info here).

Steps to reproduce the bug

Create a new Node project, attempt to import unleash-proxy-client using require, and start the application. Notice the error, ex:

const unleash_proxy_client = require("unleash-proxy-client");

Expected behavior

Using CommonJS to import the package should not result in a runtime error.

Logs, error output, etc.

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/samuel/WSL_Dev/Personal/dokku_app/node_modules/.pnpm/unleash-proxy-client@3.0.0/node_modules/unleash-proxy-client/build/cjs/index.js from /home/samuel/WSL_Dev/Personal/dokku_app/src/server/dist/shared/modules/stores/feature_flags_store.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/samuel/WSL_Dev/Personal/dokku_app/node_modules/.pnpm/unleash-proxy-client@3.0.0/node_modules/unleash-proxy-client/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/home/samuel/WSL_Dev/Personal/dokku_app/src/server/dist/shared/modules/stores/feature_flags_store.js:4:32)
    at Object.<anonymous> (/home/samuel/WSL_Dev/Personal/dokku_app/src/server/dist/server/src/boot_server.js:15:31)
    at /home/samuel/WSL_Dev/Personal/dokku_app/src/server/dist/server/src/main.js:29:43 {
  code: 'ERR_REQUIRE_ESM'
}

Screenshots

No response

Additional context

No response

Unleash version

3.0.0

Subscription type

None

Hosting type

None

SDK information (language and version)

No response

sjaanus commented 8 months ago

Hey @gamedevsam! 😊 Thanks so much for pointing that out. We've worked on a fix and it's now up in the beta version. Fancy giving it a whirl? Here's the link. Let us know how it goes, and if all's good, we'll get that new version out to everyone. Cheers!

gamedevsam commented 8 months ago

Your fix worked for me, thanks for turning this around so quickly. Feel free to close this issue when you guys push out the release.

sjaanus commented 8 months ago

Hey there! 😊 Just wanted to let you know that we've rolled out version 3.1.0 as a stable release. You can check it out right here: https://www.npmjs.com/package/unleash-proxy-client/v/3.1.0. Cheers! 🚀