使用 Forge 模块时未定义错误 window

Error window is not defined when using Forge module

我是 NativeScript 的新手,我正在尝试在项目中进行加密。 我打算使用 Forge 模块并使用 npm install --save node-forge 来安装锻造。但是一旦我添加了 import 语句 (var forge = require("node-forge"); ) 在 home-page.js 文件中出现此错误。window 未定义

堆栈跟踪:

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

有人知道这个问题吗?

您可能只能在 NativeScript 中使用 CommonJS 模块。任何浏览器/节点特定的功能都不会在 NativeScript 环境中工作。

此包使用 window.forge 来保持全局引用,window 确实是浏览器特定的功能。但由于它仅用于存储全局引用,所以一个技巧是在加载此包之前创建全局 window 对象。

global.window = {};

但是这个包似乎还使用了其他浏览器特定的功能,例如加载 SWF/ActionScript,这些没有任何解决方法。您必须为 iOS / Android 找到相同的本地等效项。