TypeScript and ESM
See original GitHub issueTypeScript example doesn’t work for my project with:
- Sources in JS ESM (to avoid compiler)
- Tests in TypeScript ESM
"type": "module"
inpackage.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:
- Created 2 years ago
- Reactions:2
- Comments:22 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just finished putting together
loadr
, which is thethingy
in my snippet above 😆With it, @alextes’ example can be change from:
to
It just needs a
loadr.mjs
configuration file:Of course, your
test
command could also include theloadr
command directly: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 😆another way would be using esbuild-node-loader:
I currently use that in most of my libraries