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

compromise_1.default is not a function #687

Closed
weiying-chen opened this issue Jan 28, 2020 · 14 comments
Closed

compromise_1.default is not a function #687

weiying-chen opened this issue Jan 28, 2020 · 14 comments

Comments

@weiying-chen
Copy link

weiying-chen commented Jan 28, 2020

This is what I did:

npm i ts-node
npm i typescript
npm i compromise

Then I pasted the node module example:

// script.ts
import nlp from 'compromise'

var doc = nlp('London is calling')
doc.verbs().toNegative()

Then did:

ts-node script.ts

And I got this error:

TypeError: compromise_1.default is not a function

The same happened when I was working in a VS Code extension.ts file. (So maybe Compromise has problems running in TypeScript files?). Compromise worked well after I changed import nlp from 'compromise' to const nlp = require('compromise').

Note: These are the versions of the packages.

"compromise": "^12.3.0",
"ts-node": "^8.6.2",
"typescript": "^3.7.5"
@Drache93
Copy link
Contributor

Drache93 commented Jan 28, 2020

Hey @alexcheninfo, I just tested with the same setup and a fresh tsconfig; seems to be working fine on my end. Have you made any changes to your tsconfig?

compromise definitely has a default export, but for now a workaround might be adding "allowSyntheticDefaultImports": true, to your tsconfig

@weiying-chen
Copy link
Author

weiying-chen commented Jan 28, 2020

@Drache93 Thanks for helping.

I don't have a tsconfig file in the folder I created to recreate this error.

This is how my tsconfig file looks like in my VS Code extension:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "out",
    "lib": [
      "es6"
    ],
    "sourceMap": true,
    "rootDir": "src",
    "strict": true,
    /* enable all strict type-checking options */
    /* Additional Checks */
    // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
    // "noUnusedParameters": true,  /* Report errors on unused parameters. */
    "allowSyntheticDefaultImports": true // I just added this
  },
  "exclude": [
    "node_modules",
    ".vscode-test"
  ]
}

As you can see, I added allowSyntheticDefaultImports": true, but I'm still getting the same error.

VS Code is also showing another problem. It's in compromise/types/index.d.ts. The error says Cannot find name 'Text'. It's this line:

out(format: 'debug'): Text

Not sure if that has anything to do with the original problem.

@Drache93
Copy link
Contributor

Drache93 commented Jan 28, 2020

No problem! Ah right, just tried your config definitely the problem.

Played around, looks like this boy here fixes your original problem (is included in default tsconfig - tsc --init)

    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

@weiying-chen
Copy link
Author

@Drache93 That did it. Thanks, this problem had been bugging me for a while!

@Drache93
Copy link
Contributor

@alexcheninfo Glad I could help!

Drache93 pushed a commit to Drache93/compromise that referenced this issue Feb 6, 2020
@twgraham
Copy link

twgraham commented Feb 9, 2020

Is setting esModuleInterop really necessary? I use a whole bunch of library's with default exports, but I import them as import * as lib from 'library'. I can't currently do this with compromise because then the types don't line up. It's kind of annoying that this is the only library I have to enable esModuleInterop for and then have to change existing imports for the other libraries with default exports.

@Drache93
Copy link
Contributor

Drache93 commented Feb 9, 2020

Hi @twgraham thanks for the feedback.
Compromise does have a default export so I can look into why typescript seems to have a problem with it (aka why es modules is required). Since esModulesInterop is default I assumed it was safe.

What's the effect on your other libraries? * import doesn't work the same?
Compromise is the only library I've seen that namespaces the types and overlaps that namespace with the default export... probably worth changing this to just direct exports so they can be used with asterisk or deconstruction - that said, compromise has lots of classes that we want the types, but the constructor is never exported, hence the use of a namespace.

Could we reopen this please @spencermountain ?

@twgraham
Copy link

Sorry for the delay in responding @Drache93. Don't mind me, just having a whinge. esModuleInterop isn't the default, but I found out it is recommended. It is default if you use something like babel-loader to compile your typescript though.

The issue I saw was that all existing star imports would fail when esModuleInterop was enabled e.g. import * as React from 'react' import * as cx from 'classnames' etc.

Compromise is the only library I've seen that namespaces the types and overlaps that namespace with the default export... probably worth changing this to just direct exports so they can be used with asterisk or deconstruction

I don't really want to suggest rewriting your types, especially when the solution suggested is the recommended solution by TS themselves. I'll suck it up and rewrite all my imports 👍

@colinricardo
Copy link

colinricardo commented Apr 26, 2020

@Drache93

I'm getting this error:

$ tsc -p .
node_modules/compromise/types/index.d.ts:315:27 - error TS2304: Cannot find name 'Text'.

315     out(format: 'debug'): Text

when doing:

import nlp from "compromise";

With the following in my tsconfig.json:

"allowSyntheticDefaultImports": true,
"esModuleInterop": true 

Am I missing something?

If I change the relevant types file to have:

out(format: 'debug'): string[]

Everything works fine.

@Drache93
Copy link
Contributor

Drache93 commented Apr 27, 2020

Ah nice find. Looks like Text was a bad type - actually a built in one so it passed tests!
.out('debug') and .debug() both return your Document.

Fixed here: #749

@colinricardo
Copy link

@Drache93 Ah great, thanks!

@admehta01
Copy link

Anyone know of any fork where someone has fixed the types in this library so we don't need to change our imports?

@spencermountain
Copy link
Owner

hey Anish, i can push a hotfix release for the type change that changed in vscode recently.

@admehta01
Copy link

Hm I think that may be unrelated to this issue. I meant fixing the types files so we don't get the compromise_1.default is not a function error..

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

No branches or pull requests

6 participants