0

I am new to NativeScript and I am trying to do encryption in the project. I intended to use the Forge module and used npm install --save node-forge to install the forge. But once I added import statement (var forge = require("node-forge"); ) in the home-page.js file I got this error.window is not defined

stack trace:

Webpack compilation complete. Watching for file changes.
Webpack build done!
Successfully transferred bundle.651345b9e00673263e5e.hot-update.js on device emulator-5554.
Successfully transferred vendor.651345b9e00673263e5e.hot-update.js on device emulator-5554.
Successfully transferred 651345b9e00673263e5e.hot-update.json on device emulator-5554.
JS: HMR: Checking for updates to the bundle with hmr hash 651345b9e00673263e5e.
Refreshing application on device emulator-5554...
JS: HMR: Ignored an error while updating module ./home/home-page.js <self-accept-errored>
JS: HMR: ReferenceError: window is not defined
JS: HMR: Cannot apply update with hmr hash 651345b9e00673263e5e.
JS: HMR: window is not defined
Successfully transferred bundle.js on device emulator-5554.
Successfully transferred runtime.js on device emulator-5554.
Successfully transferred vendor.js on device emulator-5554.
Restarting application on device emulator-5554...
Successfully synced application org.nativescript.HelloGamma on device emulator-5554.
JS: HMR: Hot Module Replacement Enabled. Waiting for signal.
System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to start activity ComponentInfo{org.nativescript.HelloGamma/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err:  > window is not defined
System.err:
System.err: StackTrace:
System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.HelloGamma/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err:  > window is not defined
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     at android.os.Looper.loop(Looper.java:164)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
System.err: Caused by: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err:  > window is not defined
System.err:     at com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1209)
System.err:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:1096)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1083)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1063)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1055)
System.err:     at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:19)
System.err:     at android.app.Activity.performCreate(Activity.java:7009)
System.err:     at android.app.Activity.performCreate(Activity.java:7000)
System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
System.err:     ... 9 more

Does anyone know about this issue?

1 Answer 1

0

You may only able to use CommonJS modules in NativeScript. Any Browser / Node specific features will not work within NativeScript environment.

This package uses window.forge to keep a global reference, window is indeed a browser specific feature. But since it's used only for storing global reference, a trick is to create global window object before loading this package.

global.window = {};

But this package also seems to use other browser specific features like loading SWF / ActionScript, those don't have any workaround. You will have to find a native equivalent of same for iOS / Android.

2
  • 1
    Thanks, @manoj for the response.I followed [this] (nativescript.org/blog/…) to be able to use npm packages in nativescript, but it had a lot of issues in nativescript 6 so I left it. I decided to try adding crypto in the native layer. Sep 17, 2019 at 4:38
  • As I already said, even if you follow the steps it's not all the modules.
    – Manoj
    Sep 17, 2019 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.