typescript-starter icon indicating copy to clipboard operation
typescript-starter copied to clipboard

Option to work with ESM (ES Module) only libraries like node-fetch

Open chriszrc opened this issue 3 years ago • 0 comments

  • I'm submitting a ... [ ] bug report [ x ] feature request [ ] question about the decisions made in the repository [ ] question about how to use this project

  • Summary Currently, if you add a dependency like node-fetch, it breaks the build/run/test because module is set to commonjs. There is a non trivial amount of configuration to actually reconfigure the project for ESM.

    First you have to setup the project to build esm, which is easy enough because node-fetch gives you the steps:

    https://github.com/node-fetch/node-fetch/issues/1279#issuecomment-915062146

    However ava requires additional reconfiguration. For ava, you need to:

  • bump the ava dependency to ^4.1.0 - https://github.com/avajs/ava/blob/main/docs/recipes/es-modules.md

  • bump the ava typescript dependency to ^3.0.1 and add the "compile": false property- https://github.com/avajs/typescript/blob/v3.0.1/README.md#enabling-typescript-support

  • as mentioned here though (https://github.com/avajs/ava/issues/2593#issue-714117054), by default ava will still be looking for files with the .js extension, which isn't what you get from tsc. To finally have the tests pass again, you need to enable --experimental-specifier-resolution=node in the ava config:

    "ava": {
     ....
     "nodeArguments": [
      "--experimental-specifier-resolution=node"
      ],
    }
    

chriszrc avatar Mar 16 '22 13:03 chriszrc