【问题标题】:Error window is not defined when using Forge module使用 Forge 模块时未定义错误窗口
【发布时间】:2020-01-16 23:19:07
【问题描述】:

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

堆栈跟踪:

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

有人知道这个问题吗?

【问题讨论】:

    标签: android encryption nativescript forge


    【解决方案1】:

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

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

    global.window = {};
    

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

    【讨论】:

    • 感谢@manoj 的回复。我关注 [this] (nativescript.org/blog/…) 以便能够在 nativescript 中使用 npm 包,但它在 nativescript 6 中有很多问题,所以我离开了.我决定尝试在原生层中添加加密。
    • 正如我已经说过的,即使您按照步骤操作,也不是所有模块。
    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2020-08-30
    • 2016-05-24
    相关资源
    最近更新 更多