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

Cannot use 'new' with an expression whose type lacks a call or construct signature. #98

Closed
FaganSC opened this issue Feb 28, 2019 · 23 comments

Comments

@FaganSC
Copy link

FaganSC commented Feb 28, 2019

Trying to use this NPM Package in a SharePoint ReactJS Framework project and when I try to use it, I get "Cannot use 'new' with an expression whose type lacks a call or construct signature."

I'm trying the NodeJS example to start with and it's erroring on the following line:

var parser = require('rss-parser');
let parser = new Parser();

I have also tried import { Parser } from 'rss-parser'; in the project and it finds the module but get this as well:

Module '"../../../../../spfx/node_modules/rss-parser"' has no exported member 'Parser'.

Any Thoughts?

@rbren
Copy link
Owner

rbren commented Feb 28, 2019

Thanks for the report. Is this using version 3.6.3?

Some folks using React seem to have trouble importing. You can check out #53 and #79 for more info and workarounds.

We also just updated the index.d.ts file in 3.6.3 - if you're using typescript at all you might want to try 3.6.2 to see if that helps.

@rbren
Copy link
Owner

rbren commented Feb 28, 2019

You might also want to try import {RSSParser} from 'rss-parser'

@FaganSC
Copy link
Author

FaganSC commented Feb 28, 2019

Thanks @bobby-brennan !!

Yes it's version 3.6.3. So, I'll take a look at those workarounds and try 3.6.2 as well.

I did try import {RSSParser} from 'rss-parser' and got
Module '"../../../../../spfx/node_modules/rss-parser"' has no exported member 'RSSParser' as well. with 3.6.3

@FaganSC
Copy link
Author

FaganSC commented Feb 28, 2019

Just to update you, I tried 3.6.2 and got the same errors. I haven't tried the workarounds yet

@ninoguba
Copy link

Same here, still couldn't get it to work both 3.6.2 and 3.6.3.

Tried:
import {RSSParser} from 'rss-parser'

Then:

import * as Parser from 'rss-parser';
let parser = new Parser();
parser.parseURL(this.PODCASTS_URL).then((podcasts : Array<any>)=>{ this.podcasts = podcasts; })

I'm getting these errors:

ERROR in ./node_modules/rss-parser/lib/parser.js
Module not found: Error: Can't resolve 'http' in ...
ERROR in ./node_modules/rss-parser/lib/parser.js
Module not found: Error: Can't resolve 'https' in ...

I'm trying to use this with Angular and Ionic 4.

@FaganSC
Copy link
Author

FaganSC commented Feb 28, 2019

I just tired something similar to @ninoguba with import * as Parser from 'rss-parser'; and it was able to find the module but still getting Cannot use 'new' with an expression whose type lacks a call or construct signature. when trying let parser = new Parser();

@FaganSC
Copy link
Author

FaganSC commented Feb 28, 2019

Also tried the workaround #53 with using import * as Parser from 'rss-parser/dist/rss-parser.min.js';

It complied but got a runtime error of
Uncaught (in promise) TypeError: rss_parser_min is not a constructor

@ghost
Copy link

ghost commented Mar 1, 2019

@FaganSC are you using TypeScript, or are you using JavaScript?

@FaganSC
Copy link
Author

FaganSC commented Mar 2, 2019

@benji7425 Typescript. It's a SharePoint Framework project I'm working on.

@ghost
Copy link

ghost commented Mar 2, 2019

And is it a runtime error or compile time error you're seeing? Can you post the generated java script if you can build?

@ninoguba
Copy link

Even with the latest 3.7.0 version, I'm still getting this compile error:

ERROR in src/app/services/podcasts.service.ts(2,10): error TS2305: Module '"/Users/me/Documents/app/node_modules/rss-parser/index"' has no exported member 'RSSParser'.

When using this to import:

import { RSSParser } from 'rss-parser';

Too bad, being able to use this would have been great but moving on for now...

@chrizy
Copy link

chrizy commented Mar 27, 2019

Hi, would love to get this working with Angular 7, cant get the import to work correctly. (tried using version 3.7.0 with all the above suggestions)
Thanks

@tofra
Copy link

tofra commented Apr 17, 2019

I'm also wanting to use this rss-parser, but i'm getting the same errors as @ninoguba using Angular/Ionic4

@ghost
Copy link

ghost commented Apr 26, 2019

You should be able to use import * as Parser from "rss-parser" to solve the issue. TypeScript v3.4.5 compiles this to const Parser = require("rss-parser");, which is correct.

Make sure you have moduleResolution set to node in your tsconfig.json, as "classic" won't search in node_modules I believe.

@tofra
Copy link

tofra commented Apr 29, 2019

I have set moduleResolution: "node".
When I use: import * as Parser from "rss-parser"; I get this error

[ng] ERROR in src/app/services/feed.service.ts(49,36): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

When I change it to import Parser from 'rss-parser'; I get this error

[ng] WARNING in ./node_modules/xml2js/node_modules/sax/lib/sax.js
[ng] Module not found: Error: Can't resolve 'stream' in '/path-to-project/node_modules/xml2js/node_modules/sax/lib'
[ng]
[ng] ERROR in ./node_modules/rss-parser/lib/parser.js
[ng] Module not found: Error: Can't resolve 'http' in '/path-to-project/node_modules/rss-parser/lib'
[ng] ERROR in ./node_modules/rss-parser/lib/parser.js
[ng] Module not found: Error: Can't resolve 'https' in '/path-to-project/node_modules/rss-parser/lib'
[ng] ERROR in ./node_modules/xml2js/lib/parser.js
[ng] Module not found: Error: Can't resolve 'timers' in '/path-to-project/node_modules/xml2js/lib'

Versions I tried: 3.5.4, 3.6.3 and 3.7.0

Do you have an idea? I can setup a git repo so you can reproduce if you want.

@ghost
Copy link

ghost commented Apr 29, 2019

If you can provide a git repo to reproduce I would be happy to take a look @tofra

@tofra
Copy link

tofra commented Apr 30, 2019

@ghost
Copy link

ghost commented May 1, 2019

I've taken a look at your demo @tofra, I believe this issue is related to Angular's module resolution rather than anything to do with this repository. I don't have experience with Angular, so can't help you solve these issues, but your Angular config seems unable to resolve some core node modules. Specifically "http", "https" and "timers".

This can be demonstrated by trying to import "https" yourself. Remove all references to "rss-parser" from your project so that it compiles fine. Then add import * as https from "https", and you get the same error about being unable to resolve the module, demonstrating it's not an error specifically with this project.

To further validate that the current rss-parser typings are working fine with your project, you can compile directly with typescript and check the outputted javascript. Using ./node_modules/bin/tsc to compile to JS yields no issues, and generates correct code for importing and referencing this module.

The issue is with your angular config being unable to resolve various node modules, which this library relies upon.

@tofra
Copy link

tofra commented May 1, 2019

yes, you are correct, the "can't resolve issue" can be fixed using #109 (comment)

But still I have the issue with
[ng] ERROR in src/app/services/feed.service.ts(49,36): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

I haven't found a solution for it yet.

@ghost
Copy link

ghost commented May 1, 2019

Let's continue this discussion in your project so as not to pollute this issue. We can update here if we find the cause.

@ghost
Copy link

ghost commented May 4, 2019

Update: looks like this is caused by using 'esModuleInterop' with your TypeScript. As far as I can tell this project implements CommonJS module standards which violate new ECMAScript module standards. 'esModuleInterop' instructs TypeScript to error if trying to implement a module in a non-ES-valid way. There is also #96 requesting support for ES modules.

@Andreew4x4
Copy link

Andreew4x4 commented Oct 11, 2019

HI all,
I've had same problem. And that solution worked for me:

import rssParser from 'rss-parser';

const parser = new rssParser();

    (async () => {

      const feed: any = await parser.parseURL('https://www.reddit.com/.rss');
      console.log(feed.title);

      feed.items.forEach((item: any) => {
        console.log(item.title + item.link);
      });

    })();

@rbren
Copy link
Owner

rbren commented Jan 28, 2020

I'll assume the above is the right solution. Please reopen if that doesn't work for anyone.

@rbren rbren closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants