Search code examples
momentjsnativescriptrequire

NativeScript Playground: moment.js npm package included but error requiring moment


I just started experimenting with Nativescript and am using the Playground to test things and see how it works. What I wanted to do: add the moment.js module for formatting date/time What I tried: 1. added the moment package. This appears to have worked because Playground now shows the moment folder along with files (package.json, ender.js, moment.js, etc) and subfolders. 2. In my code I used this snippet to require "moment"

var Moment = require("moment");

This failed though because I get an error of

Error: Could not find module 'moment'. Computed path '/var/mobile/Containers/Data/Application/xxxx/Documents/Playground/LiveSync/app/tns_modules/moment'

Any suggestions on what I need to change to get it to find 'moment'? I checked in package.json and it has the name as "moment".


Solution

  • The default require statement will search for the module form the tns_modules directory that was packed with the app during build time. So with Playground you could use relative path.

    For example, if you want to use it on app.js which is in the root level,

    var Moment = require('./moment');