question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript and ESM

See original GitHub issue

TypeScript example doesn’t work for my project with:

  1. Sources in JS ESM (to avoid compiler)
  2. Tests in TypeScript ESM
  3. "type": "module" in package.json

Repository to reproduce the error.

$ git clone git@github.com:ai/nanodelay.git
$ git checkout uvu
$ yarn
$ yarn test
uvu -r ts-node/register test && eslint . && check-dts && size-limit && yaspeller *.md
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/ai/Dev/nanodelay/test/index.test.ts
require() of ES modules is not supported.
require() of /home/ai/Dev/nanodelay/test/index.test.ts from /home/ai/Dev/nanodelay/node_modules/ts-node/dist/index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/ai/Dev/nanodelay/package.json.

    at createErrRequireEsm (/home/ai/Dev/nanodelay/node_modules/ts-node/dist-raw/node-cjs-loader-utils.js:86:15)
    at assertScriptCanLoadAsCJSImpl (/home/ai/Dev/nanodelay/node_modules/ts-node/dist-raw/node-cjs-loader-utils.js:19:11)
    at Object.require.extensions.<computed> [as .ts] (/home/ai/Dev/nanodelay/node_modules/ts-node/src/index.ts:1288:5)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at /home/ai/Dev/nanodelay/node_modules/uvu/run/index.js:9:3
    at Array.forEach (<anonymous>)
    at Object.exports.run (/home/ai/Dev/nanodelay/node_modules/uvu/run/index.js:6:9)

Removing "type": "module" from package.json solves the problem, but break my no compiler workflow.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
lukeedcommented, Aug 17, 2021

I just finished putting together loadr, which is the thingy in my snippet above 😆

With it, @alextes’ example can be change from:

$ node --experimental-loader ts-node/esm node_modules/uvu/bin.js src '.*.test.ts'

to

$ loadr -- npm test
# assumes "test": "uvu src '.*.test.ts'"

It just needs a loadr.mjs configuration file:

export const loaders = ['ts-node/esm'];

Of course, your test command could also include the loadr command directly:

// package.json
{
  "scripts": {
    "test": "loadr -- uvu src '.*.test.ts'"
  }
}

This doesn’t change anything about the original ESM + TS issue, but it’s just a tool to cleanup the absurd amount of CLI paths and/or --experimental-loader flags you’re throwing around 😆

5reactions
talentlessguycommented, Jul 7, 2021

another way would be using esbuild-node-loader:

node --experimental-loader esbuild-node-loader node_modules/uvu/bin.js tests

I currently use that in most of my libraries

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - ECMAScript Modules in Node.js - TypeScript
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to...
Read more >
TypeScript and native ESM on Node.js - 2ality
In this blog post, I'll explain everything you need to know in order to use and produce native ECMAScript modules on Node.js.
Read more >
Understanding TypeScript 4.7 and ECMAScript module support
The TypeScript 4.7 release comes a major upgrade to ECMAScript module ... 14 support for ECMAScript modules (also known as “ESM”) landed.
Read more >
ESM TypeScript Usage - GraphQL Code Generator
ESM TypeScript Usage. ESM is an important step in the JavaScript ecosystem because it allows static analysis of the dependency tree.
Read more >
How to import esm modules with NodeJS 13, and Typescript ...
But no, in Typescript you can declare and initialize properties outside the constructor. Typescript will just transpile them in javascript by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found