I'm not sure this issue is related to pubnub or ts-node, but when I use pubnub with ts-node together, it will throw out the following error:
TypeError: pubnub_1.default is not a constructor
My code is:
import Pubnub from "pubnub"
const pubnub = new Pubnub({
subscribeKey: "sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f"
})
And my tsconfig.json is:
{
"compilerOptions": {
"strict": true,
"lib": ["es6"],
"typeRoots": [
"./node_modules/@types/",
"./src/@types/"
]
}
}
I've already installed types for pubnub,
npm install pubnub --save
npm install @types/pubnub --save-dev
I guess I need to change some configurations in tsconfig.json, any ideas?
The correct import is import pubnub = require('pubnub').
@NaridaL By doing so you're importing it as a normal JavaScript library and you lose type checking
https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
@NaridaL Thanks!
When importing a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
But in this file types/pubnub/index.d.ts, pubnub is exported by export default Pubnub; instead of export = Pubnub;
Check this pull request DefinitelyTyped/DefinitelyTyped#23085, the export statement is now updated to export = Pubnub
@kantlove Thanks, it does solve the problem!
Hi,
I can't get this working in my Jest tests. Seems to work fine in my app, but when testing I get TypeError: pubnub_1.default is not a constructor.
I am unable to import via import pubnub = require('pubnub') because then I get:
'import =' is not supported by @babel/plugin-transform-typescript
Please consider using 'import <moduleName> from '<moduleName>';' alongside Typescript's --allowSyntheticDefaultImports option.
And of course I cannot do as it suggests, because PubNub doesn't like it.
What is the recommended solution?
Thanks
Most helpful comment
The correct import is
import pubnub = require('pubnub').