Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript import error on node #73

Closed
shepherdwind opened this issue Dec 12, 2017 · 5 comments
Closed

TypeScript import error on node #73

shepherdwind opened this issue Dec 12, 2017 · 5 comments
Assignees
Labels

Comments

@shepherdwind
Copy link

shepherdwind commented Dec 12, 2017

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.

@scurker
Copy link
Owner

scurker commented Dec 12, 2017

Typescript is currently tested with imports.

Would you be able to share your tsconfig.json and webpack.config.js? I'm not able to reproduce your issue.

@shepherdwind
Copy link
Author

shepherdwind commented Dec 13, 2017

You example is reproduceable .

I just download you code , then run npm i && node index.js. On my compute, this step will throw error

/Downloads/1fd2ec351c01b019063b48e53803e53d-c6b1134ca6ed994fbd482d226d4f0d0f60f07559/index.js:4
currency_js_1.default(1).add(1.1);
                     ^

TypeError: currency_js_1.default is not a function
    at Object.<anonymous> (Downloads/1fd2ec351c01b019063b48e53803e53d-c6b1134ca6ed994fbd482d226d4f0d0f60f07559/index.js:4:22)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)

@scurker
Copy link
Owner

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

@scurker scurker self-assigned this Dec 13, 2017
@scurker scurker added the bug label Dec 13, 2017
@shepherdwind
Copy link
Author

If I use import * as like this:

import * as currency from 'currency.js';

tsc will throw error [ts] Cannot invoke an expression whose type lacks a call signature. .

I think the cjs module should change to exports.default = currency; . Or for version compatibility, module.exports = currency; module.exports.default = currency; will be more better.

But I don't know how to realize this on rollup, So I can't to send pr to fix this.

@scurker
Copy link
Owner

scurker commented Dec 14, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants