Search code examples
nativescriptrequire

nativescript Cannot find module 'platform'


In Nativescript after-prepare hook, I want to find out the current platform that the nativescript is preparing for so that I can do different things. So I added

require("platform");

But when I do tns prepare android, I always get the following error:

Error: Cannot find module 'platform'

When I tried adding require("http"); above the require("platform");, require("http");does not give me error. What is the difference between these two cases? Why require("platform"); fails but require("http"); succeed?

Both http module and platform module are in tns-core-modules folder.


Solution

  • You cannot use the TNS core modules inside the hooks. Depending on what your trying to do maybe it would be better to use gulp or something like it. Then create scripts in your package.json to do the processes you need for either platform and then run the nativescript process.

    for example.

    package.json

    scripts": {
        "android-stuff": "gulp yourTask && tns run android",