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 example: Can't use ES modules imports with Node.js AND uvu

See original GitHub issue

Thanks for the tool @lukeed!

I’m having an issue with the Typescript example:

I’m trying to use ES modules directly, with the latest version of Node.js, but I can’t make test modules to work at the same time, when using imports between source modules.

I added the following (which I think it’s a pretty common use case), to the typescript example you provided:

  • Added "type": "module" to package.json file, so I can use ES modules with Node.js
  • Added src/main.ts file with the following content:
import { sum } from './math'

export function run() {
    console.log(sum(2, 3))
}

run()
  • Added tests/main.ts file with the following content:
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import * as main from '../src/main';

test('run', () => {
    assert.type(main.run, 'function');
});
  • Build and test:
yarn build && yarn test

Tests work! But when I try to run emmited files with Node.js, got the following error:

node build/main.js
internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/cristian/projects/uvu-typescript-example/build/math' imported from /home/cristian/projects/uvu-typescript-example/build/main.js

This is because ES modules imports need explicit file extension. So I fix the issue adding imports file extensions to src/main.ts (which is supported by Typescript)

import { sum } from './math.js'
...

Now building and running main file works:

yarn build && node build/main.js
yarn run v1.22.4
$ tsc
Done in 2.44s.
5

but tests don’t 😦 (got the following error):

yarn test
yarn run v1.22.4
$ uvu -r ts-node/register tests
(node:28693) UnhandledPromiseRejectionWarning: Error: Cannot find module './math.js'

Tried different tsconfig.json configurations, but can’t make the emmited files and test files to work at the same time.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, Jul 15, 2020

Oh, sorry – forgot the second half of your predicament. TypeScript can’t output mjs files (which type: "module" addresses) nor modify the output links (which is still unaddressed & causes the error you see).

I’d recommend passing everything through Rollup – here’s an example TS+Rollup setup.

But if you want to use this example as your boilerplate, you can do so by following this guide: https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714

You’re only missing step 3: --experimental-specifier-resolution=node With that, you invoke the OG module-resolution algorithm which allows you to have extension-less imports

$ node --experimental-specifier-resolution=node build/main.js
#=> 5
0reactions
lukeedcommented, Jul 15, 2020

You’re welcome, happy to help! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Cannot use import statement outside a module
Original answer (2020): Adding "type": "module" to package. json will tell Node you are using ES2015 modules, which should get rid of the...
Read more >
Use ES Modules in Node.js & Fix "cannot use import ...
This article explains ECMAScript modules in Node.js, and how to use import and export modules in Node.js. Fix cannot use import statement outside...
Read more >
Import and Export ES Modules in Node.js using TypeScript ...
Use ES Modules in Node.js without an experimental flag by configuring Babel and TypeScript to support ES Module import and export syntax.
Read more >
esmock - npm
provides native ESM import mocking for unit tests. Latest version: 2.1.0, ... Start using esmock in your project by running `npm i esmock`....
Read more >
Starting a TypeScript Project in 2021 - Hacker News
Not sure if uvu is any good for Typescript though. ... switch to it on the frontend, but we're using it for our...
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