Unexpected token import


#1

Hi,

I’m trying to use the Exit plugin, here’s what I do:

  • tns create exitApp
  • cd exitApp\
  • npm install nativescript-exit --save

I only add:
import {exit} from ‘nativescript-exit’;
to the top of the main-page.js file, and then after:

  • tns run android

I get an error: unexpected token import


#2

Do you really need a plugin to do this? you can do it directly in your code

var application = require("application");
if (application.android) {
    application.android.foregroundActivity.finish();
} else if (application.ios) {
    exit(0);
}

here is a quick playground


#3

I need to get the job done :slight_smile:

Your solution is interesting, but it seems to minimize the app, not actually kill it (at least on my android emulator)


#4

The plugin you mentioned also uses just one line of code, you may try that directly.

android.os.Process.killProcess(android.os.Process.myPid());

Besides, you had mentioned that you used

import {exit} from ‘nativescript-exit’;

in main-page.js. As @Pete.K mentioned at ECMAScript 6 (ES6) importing problems Android Runtime doesn’t support the fancy es6 import statement. You have to use require or migrate to TypeScript.


#5

Seems to be working, so I guess the final code will be:

if (application.android) {
        android.os.Process.killProcess(android.os.Process.myPid());
    } else if (application.ios) {
        exit(0);
    }  

#6

And if you want to require that plugin in JS,
it would be

const exit = require("nativescript-exit").exit;

#7

Thanks, but is there really a need for using a plugin for it if we have a few lines of code doing the exit? Do you think it’s better to use a plugin?


#8

No because the lines above are same as in the plugin

look here is android implementation in the plugin

and here is the iOS

its literally only two lines plugin :slight_smile:


#9

Great, so my problem with the “unexpected token import” was related to using TS specific code in a core app, instead of a require. But in the end the exit functionality can be solved much easier, without plugins.


#10

Yeah, I agree with @ganas
There is no need to use that plugin