Tenoda
Tenoda

Reputation: 193

"moment is undefined" when launching angular app in node-webkit

I want to launch my angular application which works in general, but when I get to use moment I got the error that "moment" is undefined.

I am using "angular-moment" from here

var app = angular.module("MyApp",
[
    "ngRoute",
    "ui.bootstrap",
    "angularMoment",
    'angular-jwt',
    'angular-storage'
]);

My package.json looks like this

{
  "name": "myapp",
  "main": "index.html",
  "toolbar":"true",
  "dependencies": {
    "moment": "*"
  }
}

I am trying to use it with

app.config(function (moment) {
    moment().format();
});

which says that moment ist undefined.

How do I have to modify my package.json to get node-webkit find moment? Or Angular-Moment? Thanks in advance.

Upvotes: 0

Views: 3835

Answers (2)

gzc
gzc

Reputation: 8629

I encountered the same problem, I use this code snippet to solve it. You should replace vendor.js with your own files suce as angular, moment.

<script>
//hide global object
try {
    window.globalTmp = global;
    global = undefined;
} catch (e) {}
</script>
<script src="vendor.js"></script>
<script>
//recover global object
try {
    global = window.globalTmp;
    window.globalTmp = undefined;
} catch (e) {}
</script>

moment is undefined because it's added to global other than window. global is an object of node-webkit.If you type global in console, you will find global.moment in output.

I found this snippet in moment's source code which can support my explain.

var moment,
    VERSION = '2.8.4',
    // the global-scope this is NOT the global object in Node.js
    globalScope = typeof global !== 'undefined' ? global : this,

Upvotes: 0

Max
Max

Reputation: 4646

Make sure you have both moment and angular-moment loading in your HTML file.

Follow the instructions on the angular-moment github page. I don't think moment().format(); is valid because moment should not be a function..

Also try including "node-remote": "<local>" in your package.json file.

Upvotes: 2

Related Questions