Skip to content

ReferenceError: navigator is not defined in Native Script 5.0 with Angular 7 #144

Open
@acharyaks90

Description

@acharyaks90

jsEncrypt not working with native script ( Angular)

import { Component, OnInit, ViewChild } from "@angular/core";
import { NavigationEnd, Router } from "@angular/router";
import { RouterExtensions } from "nativescript-angular/router";
import { DrawerTransitionBase, RadSideDrawer, SlideInOnTopTransition } from "nativescript-ui-sidedrawer";
import { filter } from "rxjs/operators";
import * as app from "tns-core-modules/application";
import * as jsEncrypt from 'jsencrypt';
@Component({
    moduleId: module.id,
    selector: "ns-app",
    templateUrl: "app.component.html"
})
export class AppComponent implements OnInit {
    private _activatedUrl: string;
    private _sideDrawerTransition: DrawerTransitionBase;

    constructor(private router: Router, private routerExtensions: RouterExtensions) {
        // Use the component constructor to inject services.
    }

    ngOnInit(): void {
        var encrypt = new jsEncrypt.JSEncrypt();
        encrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----\n' +'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANgXzYTKuBA5DRl/XKfUXzY862hKYccs9wHgQHfh7djzfICmnD67aFGoeDPS6hMJYDw8aFQjiviE+ERzKUybTA132IpVO0N1qDIZarUYFYtRWuhbQOgq/WS3soMeA43+S8s4Az0JOJiDkgOlXGYNPA5BuZkbLbzufpq3PwNS8GchAgMBAAECgYBsNodJz4abQSapNRl+RiCj9Z/haxZJ+t97RgSixDYqpcwcVNL7qzfTZK1FLoLfo5tYrtuD/wSR7WyyQoVVApPzYr0yPUeeYyY5HTog57FfEtdH+CWqwnrmHLeqhkizaaKKXFDRIyOxhWQaI+vr71XwkcEb7RsKLDGXiqf/wWyFfQJBAPhZfR+g1/2NCgc5xJ4DQetzTcK5tdKaN7Wn3rOgtBQivuPVkU7sRfL0fdWJhWRYREntAYvST1iFo42nK+/vBfsCQQDev+9mhzXJ9VKeVp0zm88SODzZBjnrE2maqkZT+VSQntFp8KUjAvB7hq3Z0sT9Zl4hYKszGkD/RXs0fGt9JmiTAkEAsJ5z7dplwVxclWbv/bAzYU9PLf0E4hHrLLWGHBld+DP9repkvMwrsu1N0F9NDl/3sNttDhMzerEP793D0Y8boQJAPVa/Vw1+u2mJ4b5aNb7/K7Y+00iKpHhJCWVATpmHNvf1FOZGoziI/osczrHiuyD4ZNqY6k6dfW4lulNUyjbMbwJBAJzrlWtMmyQp6I5YzfmDDYOh5ynolm5rr/27bsXbzdENN2O9bhV5jGqQkrGfGC+Y5ecb4GckECBajhXLlT4NQ+k=' + '-----END RSA PRIVATE KEY-----');
        var encrypted = encrypt.decrypt("QZnqxgsc6LozUMQzlLn5DNMP7APxAYQsXP1w8kMdo7S9cs4b4esVYTdFWZHD5AV/7up4E8WfXTgDoF9BQmPjT9zjdeeOMD1LIRUGcra1tdQu46MONcX4isgaJL+E5597gAqOYpSoujHGZtHM3I8tKJx9SoC+3u/RNx9bOQkcjcc=");
        console.log(encrypted);
    }

System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException:
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: "file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js, line: 2624, column: 13
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5406)
System.err: at android.app.ActivityThread.-wrap2(ActivityThread.java)
System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
System.err: at android.os.Looper.loop(Looper.java:154)
System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
System.err: Caused by: com.tns.NativeScriptException:
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: Error calling module function
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: "file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js, line: 2624, column: 13
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: ReferenceError: navigator is not defined
System.err: File: ", line: 1, column: 265
System.err:
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2624, column: 14
System.err: Frame: function:'BI_RM', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 2, column: 65
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/tns_modules/jsencrypt/bin/jsencrypt.js', line: 5, column: 2
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.component.js', line: 9, column: 17
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/app/app.module.js', line: 7, column: 23
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.sampleapp/files/app/main.js', line: 5, column: 20
System.err: Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err: at com.tns.Runtime.runModule(Native Method)
System.err: at com.tns.Runtime.runModule(Runtime.java:553)
System.err: at com.tns.Runtime.run(Runtime.java:545)
System.err: at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
System.err: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
System.err: ... 8 more

Activity

hitmacreed

hitmacreed commented on Aug 30, 2019

@hitmacreed

did u solve it? if not i can share the solution

WaitingDJ

WaitingDJ commented on Sep 23, 2019

@WaitingDJ

did u solve it? if not i can share the solution

share, please!

acharyaks90

acharyaks90 commented on Sep 23, 2019

@acharyaks90
Author
acharyaks90

acharyaks90 commented on Sep 23, 2019

@acharyaks90
Author

@hitmacreed Please share the solution

hitmacreed

hitmacreed commented on Sep 23, 2019

@hitmacreed

@WaitingDJ @acharyaks90
Go to node_modules/jsencrypt/bin/ and replace jsencrypt.js for this one
jsencrypt.zip

gogomarine

gogomarine commented on Jul 25, 2020

@gogomarine

Use this code will resolve the problem. Before import this lib, set navigator to empty object.

global.navigator={};
global.window={};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gogomarine@acharyaks90@hitmacreed@WaitingDJ

        Issue actions

          ReferenceError: navigator is not defined in Native Script 5.0 with Angular 7 · Issue #144 · travist/jsencrypt