Nodejs es6 import backendless + typescript

I am getting a "Cannot read property ‘initApp’ of undefined when I try to initialize the Backendless object.
My project is a express/typescript server-side node.js application (not a frontend browser application). This is the way I import the library into my project:

import Backendless from ‘backendless’

Could there be an issue with es6 import statement and Backendless?
Node version: v10.13.0

sample.rar (53.2 KB)

Hi Seya

Thank you for your report, I’m looking into it

Regards, Vlad

found out two issue:

  1. there was a problem with backendless.d.ts
node_modules/backendless/backendless.d.ts:1032:19 - error TS7006: Parameter 'name' implicitly has an 'any' type.

1032         getLogger(name): Backendless.Logger;
                       ~~~~

the problem has been fixed and released in backendless@5.2.8

  1. there were several issues with Browser/Dom classes

node_modules/backendless/backendless.d.ts:996:84 - error TS2304: Cannot find name ‘HTMLElement’.

996 loginWithGooglePlusSync(fields?: Object, permissions?: Object, container?: HTMLElement, stayLoggedIn?: boolean): void;

I’ve fixed it with adding “dom” library in tsconfig.json file

{
“compilerOptions”: {

“lib”: [“es2016”, “dom”],

},

}

So, as result I could import Backendless JS-SDK with es6 import/export feature and typing works as well.

ts%20-%20sample%20-%20%5B~%3ADownloads%3Asample%5D%202019-02-27%2012-27-51

Could you please

  • upgrade backendless module to v 5.2.8
  • and fix tsconfig.json
    and confirm if it works for you

Regards, Vlad

Thank you for the reply. I upgraded to ‘5.2.8’ and fixed tsconfig.json. Unfortunately I’m still getting the same error:

I am sending a new sample project with updates.

sample.rar (136.4 KB)

what command did you run, npm run build ?

I have run the following command:
yarn build

it’s strange, for me it works

Valodyas-MBP:sample valodya$ yarn install
yarn install v1.3.2
(node:62788) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[1/4] :mag: Resolving packages…
[2/4] :truck: Fetching packages…
[3/4] :link: Linking dependencies…
warning " > @tsed/swagger@5.2.1" has incorrect peer dependency “@tsed/common@5.2.1”.
[4/4] :page_with_curl: Building fresh packages…
$ npm run build

tcube.web.api@1.0.0 build /Users/valodya/Downloads/sample
npm run tsc

tcube.web.api@1.0.0 tsc /Users/valodya/Downloads/sample
tsc --project tsconfig.json

:sparkles: Done in 62.39s.
Valodyas-MBP:sample valodya$ yarn build
yarn run v1.3.2
(node:62826) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
$ npm run tsc

tcube.web.api@1.0.0 tsc /Users/valodya/Downloads/sample
tsc --project tsconfig.json

:sparkles: Done in 5.88s.
Valodyas-MBP:sample valodya$

could you please share the entire log

yes, you are running “yarn start”


Valodyas-MBP:sample valodya$ yarn start
yarn run v1.3.2
(node:62908) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
$ nodemon --watch '**/*.ts' --ignore 'node_modules/**/*' --exec ts-node src/Server.ts
[nodemon] 1.18.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: **/*.ts
[nodemon] starting `ts-node src/Server.ts`

/Users/valodya/Downloads/sample/src/Server.ts:12
Backendless.initApp("APP_KEY", "SECRET_KEY");
            ^
TypeError: Cannot read property 'initApp' of undefined
    at Object.<anonymous> (/Users/valodya/Downloads/sample/src/Server.ts:12:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (/Users/valodya/Downloads/sample/node_modules/ts-node/src/index.ts:413:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/valodya/Downloads/sample/node_modules/ts-node/src/index.ts:416:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at Object.<anonymous> (/Users/valodya/Downloads/sample/node_modules/ts-node/src/bin.ts:151:12)
[nodemon] app crashed - waiting for file changes before starting...


“start”: “nodemon --watch ‘/*.ts’ --ignore 'node_modules//*’ --exec ts-node src/Server.ts”,

most likely the problem is here –ignore 'node_modules//*'** because of backendless.d.ts file is located in ./node_modules/backendless/backendless.d.ts

yarn build works. But yarn start fails. I have removed the --ignore 'node_modules/**/*' option: still the same error. But it works if I import it with

var Bakendless = require(‘backendless’);

try to add “esModuleInterop”:true as “compilerOptions” in tsconfig.json
and you can revert " --ignore ‘node_modules/**/*’"

Yes, it worked with that option! Thanks for the assistance