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 import error on node

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
scurkercommented, Dec 13, 2017

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');

1reaction
scurkercommented, Dec 28, 2017

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')
  • add allowSyntheticDefaultImports: true to your tsconfig.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.

Read more comments on GitHub >

github_iconTop 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 >

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