Poll: Should Agent-JS deprecate CommonJS in favor of EcmaScript Modules

Discussion is taking place here: Should Agent-JS Deprecate CommonJS in v1.0.0? · dfinity/agent-js · Discussion #700 · GitHub


With a large set of changes to Agent-JS on this branch, it includes an overhaul to the codebase that enables properly generating ECMAScript Modules for the first time.

As it stands, attempting to generate both ESM and CJS fails to account for some configurations. CJS may work normally for some node versions, but not with TypeScript. I also have noticed that types can get confused between CJS and ESM when CJS is not the default module type. Given this complexity, and that these changes will be breaking anyway, should we deprecate CommonJS?

Some consequences of this in your projects may be:

  • shifting to "type": "module" in package.json if you aren’t already using it
  • changing your imports from import {HttpAgent} from '@dfinity/agent' to const {HttpAgent} = await import('@dfinity/agent')
  • Support for using the packages via CDN like unpkg
1 Like

Why do we need to use dynamic imports with this change?

Some bundlers / import systems call for the distinction if the rest of your app is built as a commonjs app.

Mainly the TypeScript compiler - I encountered this in the demo app when testing building the agent-js packages as ESM

1 Like