首页 > 解决方案 > 使用 Forge 模块时未定义错误窗口

问题描述

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

堆栈跟踪:

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

有人知道这个问题吗?

标签: androidencryptionnativescriptforge

解决方案


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

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

global.window = {};

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


推荐阅读