Import into typescript project whose option module:nodenext doesn't work
See original GitHub issueFirst 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 todist/esm
folder. However, the files of this folder still have.js
extension and thepackage.json
does’t containtype: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"
todist/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 exampleimport * from 'Require'
toimport * from 'Require/index.js'
.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top 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 >
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 Free
Top 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
@khaiquangnguyen @tgillus @Talent30 the issue has been fixed in
v4.0.0-rc.1
🚀Sure. https://github.com/khaiquangnguyen/tsbelt-nodenext Here is it.
npm run build
and thennpm run start
to see the error. So after a couple of testings, it only have issues withnodenext
module. bothesnext
andnode16
seems to work fine. This is a bit of an edge case but official node and typescript have recommendednodenext
as the path forward, so i think we should try to support this if possible.