📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

NativeScript Core

Modules

The Module Design Pattern is an integral piece of any robust application and is used to keep units of code cleanly separated and organized. NativeScript has a simple module loading mechanism which strictly follows the CommonJS Specification. Most of the functionality is similar with the Node.js implementation with some minor differences. A typical NativeScript application will use the set of modules which add cross-platform abstraction layer on top of OS-specific APIs.

NativeScript Modules vs. Node Modules

Following are the differences between Node modules and NativeScript modules:

Core Modules

NativeScript Runtime does not have built-in JavaScript modules, which enables developers to have complete control over module implementation.

Loading From tns_modules

If the module identifier passed to require(moduleName) does not begin with '/', '../', or './', then NativeScript will lookup the module within the tns_modules folder. Since there is only one such folder within an application, the lookup will happen only once, unlike in Node which recursively traverses the parent directories as well.

Global Folders

Unlike in Node, NativeScript does not have the notation for Global Modules. Modules are private for the application package. The require implementation will throw an exception in case a module from the external storage is attempted to be loaded.

See Also