nativesctip 8 does not have platform dependency

Viewed 100

it's my first project on NativeScript (ns), so I'm a bit new to it, and do not know how everything works in ns world.

For my project I setup ns+vue+ts projects with latest ns version, which is 8.1.5.

All was fine until I start adding some libraries. The first alarm bell was installing and setup of nativescript-google-maps-sdk, every tutorial and official docs telling that you need turn on SDK in a way like this (examples may differ a bit but all of them wants platform):

import * as platform from "platform";
if (platform.isIOS) { 
    GMSServices.provideAPIKey("PUT_API_KEY_HERE");
}
Vue.registerElement('MapView', () => require('nativescript-google-maps-sdk').MapView);

but my installed NativeScript does not have platfrom, instead I made it work with

import {isIOS } from "@nativescript/core";
if (isIOS) { 
    GMSServices.provideAPIKey("PUT_API_KEY_HERE");
}
Vue.registerElement('MapView', () => require('nativescript-google-maps-sdk').MapView);

Second issue which I did not solve yet is nativescript/geolocation. After I install that package I'm not able to run my ios emulator, it's failing with error:

ERROR in ./node_modules/nativescript-geolocation/geolocation.ios.js 8:15-34
Module not found: Error: Can't resolve 'platform' in '/Users/myuser/projects/app/node_modules/nativescript-geolocation'

Am I doing something wrong, or ns of version 8.1.5 not supported yet????

1 Answers

I'm just starting to migrate a very large {N} 6.8 app to 8.1 so I thought I'd check out your question as it would apply to me, too. I'm seeing the same error, but if what you need is determine the OS, you can use the Application class. See https://github.com/NativeScript/nativescript-cli/issues/5423.

Edit 12/15/2021:

As I wade into my own migration, I'm finding that you have to look at the source code to see the exports for a particular class. Platform is still there, but it's not exported directly. Instead, you have to import the various elements. Thus, this works:

import { isAndroid, isIOS } from '@nativescript/core';

This is genuinely unfortunate, as it requires considerable searching and testing, and making detailed coding changes. So far I have not found any viable migration guide. This 84-minute YouTube video is worth watching but doesn't get you into these kind of details.

If you're just starting, you'll have to be aware of the new syntax and know that most existing examples and documentation will require migration.

Edit 12/15/2021

As I continue I realize some names are simply not defined. So, platform is indeed still there, you just get to it like this:

const platform = require("@nativescript/core/platform");

Similarly, if you need file-system or KnownColors:

const fs = require("@nativescript/core/file-system");
const KnownColors = require("@nativescript/core/color/known-colors");

If you try to import a name from @nativescript/core that's not recognized you do get a list of known names in the build output. If you don't see what you're looking for there, you'll likely need to resort to a fully-qualified require (for JavaScript) or import (TypeScript).

Related