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.

Import into typescript project whose option module:nodenext doesn't work

See original GitHub issue

First of all, I want to say that I really like ts-belt utilities and really wanted to make it work with my current project.


import { A, D, O, pipe } from '@mobily/ts-belt';
         ^
SyntaxError: Named export 'A' not found. The requested module '@mobily/ts-belt' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@mobily/ts-belt';
const { A, D, O, pipe } = pkg;

Issue:

  • When import into a project whose module:nodenext, ts-belt is resolved as a esm module and redirected to dist/esm folder. However, the files of this folder still have .js extension and the package.json does’t contain type:module option, which makes node assumes that it is trying to import a CommonJS module and try to resolve the import as such.
  • Furthermore, appying the above fix doesn’t solve the problem, as it results in this error:
^^^^^^

SyntaxError: Cannot use import statement outside a module

Since all files inside esm are still understood as commonjs files, the import syntax of esm still doesn’t work.


Proposed fix:

  • Add a build step that add additional option "type":"module" to dist/esm/package.json
  • The above option will break esm folder, as node really doesn’t understand import without file extension. Typescript compiler also won’t add extensions by default. This may require manually updating ts files with import to include extension instead, for example import * from 'Require' to import * from 'Require/index.js'.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mobilycommented, Nov 29, 2022

@khaiquangnguyen @tgillus @Talent30 the issue has been fixed in v4.0.0-rc.1 🚀

2reactions
khaiquangnguyencommented, Oct 23, 2022

Sure. https://github.com/khaiquangnguyen/tsbelt-nodenext Here is it. npm run build and then npm run start to see the error. So after a couple of testings, it only have issues with nodenext module. both esnext and node16 seems to work fine. This is a bit of an edge case but official node and typescript have recommended nodenext as the path forward, so i think we should try to support this if possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Concerns with TypeScript 4.5's Node 12+ ESM Support #46452
For TypeScript 4.5, we've added a new module mode called node12 to ... Auto-imports not working: Debug failure on auto-import completion ...
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
Typescript: Cannot use import statement outside a module
ts file in node js (latest version of node.js for 07.10.19) app with importing node-module without default export. I use this construction: import...
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 >
A comprehensive guide to “Module System” in TypeScript ...
It's a normal JavaScript file with import statements to import modules and export statements to make a value (such as variable, function, class, ......
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