TypeScript import error on node
See original GitHub issueI import this package like this:
import currency from 'currency.js';
currency(1).add(1.1);
This code work fine on webpack build, but fail on node TypeError: currency_js_1.default is not a function
. I use currency.js in TypeScript project.
The npm package source exports is module.exports = currency
, which should be export = currency;
in d.ts file. So, I think maybe we should modify the currency.d.ts , or build result.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Import statement does not work in typescript - Stack Overflow
I read about import and export declarations in javascript. Then I'm trying to import a class in a file using 'import' keyword. But...
Read more >Common TypeScript module problems and how to solve them
Solution 2: Locate the module and resolve imports ; "*" value in the array means the exact name of the module, while the...
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 >Cannot use import statement outside a module [React ...
How to Fix the TypeScript SyntaxError: Cannot use import statement outside a module Error. In this section, we'll work with a basic Node ......
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 >
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
Thanks! I see the issue now. The typescript definition file seems to be correct, but typescript is attempting to import the commonjs module instead of the ES6 module. I want to investigate further to find the proper solution, but in the short term you should be able to use one of the following imports:
import * as currency from 'currency.js';
or
import currency = require('currency.js');
So here’s the root of the issue. Typescript is assuming modules export a default, which can cause issues when importing cjs modules.
As such, there seems to be only 3 possible solutions:
import * as currency from 'currency.js'
import currency = require('currency.js')
allowSyntheticDefaultImports: true
to yourtsconfig.json
I updated the typescript definition file to allow for the first 2 statements to be used, which I should hopefully be releasing in a patch update soon.