I've recently downloaded last version of moment.js and it begins to show the following message when trying to call, for example, moment().add(1, 'day');
"Deprecation warning: Accessing Moment through the global scope is deprecated, and will be removed in an uping release."
Which is the best way to call moment methonds?
Update: Figured out the problem
The problem was present because I have requirejs in my project and momentjs was trying to warn me that I should use momentjs as a module dependency instead.
The following code was extracted from momentjs v2.9.0
// CommonJS module is defined
if (hasModule) {
module.exports = moment;
} else if (typeof define === 'function' && define.amd) {
define('moment', function (require, exports, module) {
if (module.config && module.config() && module.config().noGlobal === true) {
// release the global variable
globalScope.moment = oldGlobalMoment;
}
return moment;
});
makeGlobal(true);
} else {
makeGlobal();
}
//And this is the 'makeGlobal' function. globalScope
function makeGlobal(shouldDeprecate) {
/*global ender:false */
if (typeof ender !== 'undefined') {
return;
}
oldGlobalMoment = globalScope.moment;
if (shouldDeprecate) {
globalScope.moment = deprecate(
'Accessing Moment through the global scope is ' +
'deprecated, and will be removed in an uping ' +
'release.',
moment);
} else {
globalScope.moment = moment;
}
}
So, if I use this library in a CommonJS environment, then I should use import statement.
If I use requirejs, then I should include momentjs as a dependency of my modules.
Finally, if neither the other cases acplish, then I can use it directly from global scope (window object in browser)
I've recently downloaded last version of moment.js and it begins to show the following message when trying to call, for example, moment().add(1, 'day');
"Deprecation warning: Accessing Moment through the global scope is deprecated, and will be removed in an uping release."
Which is the best way to call moment methonds?
Update: Figured out the problem
The problem was present because I have requirejs in my project and momentjs was trying to warn me that I should use momentjs as a module dependency instead.
The following code was extracted from momentjs v2.9.0
// CommonJS module is defined
if (hasModule) {
module.exports = moment;
} else if (typeof define === 'function' && define.amd) {
define('moment', function (require, exports, module) {
if (module.config && module.config() && module.config().noGlobal === true) {
// release the global variable
globalScope.moment = oldGlobalMoment;
}
return moment;
});
makeGlobal(true);
} else {
makeGlobal();
}
//And this is the 'makeGlobal' function. globalScope
function makeGlobal(shouldDeprecate) {
/*global ender:false */
if (typeof ender !== 'undefined') {
return;
}
oldGlobalMoment = globalScope.moment;
if (shouldDeprecate) {
globalScope.moment = deprecate(
'Accessing Moment through the global scope is ' +
'deprecated, and will be removed in an uping ' +
'release.',
moment);
} else {
globalScope.moment = moment;
}
}
So, if I use this library in a CommonJS environment, then I should use import statement.
If I use requirejs, then I should include momentjs as a dependency of my modules.
Finally, if neither the other cases acplish, then I can use it directly from global scope (window object in browser)
Share Improve this question edited Apr 14, 2015 at 12:44 edrian asked Dec 15, 2014 at 12:51 edrianedrian 4,5315 gold badges33 silver badges35 bronze badges 7- And this is in a clientside script ? – adeneo Commented Dec 15, 2014 at 12:54
- 1 If you're not using requireJs the last I saw was that this was an issue, re: github./moment/moment/issues/1214. It may have been resolved on master, but I don't know. – Dave Newton Commented Dec 15, 2014 at 12:58
- It seems like you're using moment for nodeJS... you could double check and get a version of moment build for the browser. – Adrian Salazar Commented Dec 15, 2014 at 12:58
- Try getting moment from cdnjs momentjs./docs/#/use-it/browser – Adrian Salazar Commented Dec 15, 2014 at 12:59
- @AdrianSalazar, I've tried using momentjs from the cdn link you posted but the problem still exists – edrian Commented Dec 15, 2014 at 13:06
2 Answers
Reset to default 10You can use requirejs to pull it in rather than using the global scope:
require.config({
paths: {
"moment": "path/to/moment",
}
});
define(["moment"], function (moment) {
moment().format();
});
Taken from http://momentjs./docs/
This really isn't an answer, but an appreciation of the problem:
There has yet to be a cogent explanation of why the deprecation occurs, or a newbie explanation of what it is. Specifically, not paragraph saying 'if you do it the old way, it breaks in a subtle way'. The closest is a bug report that, using node, a single symbol is defined in the global namespace (https://github./moment/moment/issues/1214), which is mostly philosophy.
The deprecation es on usage, so its unclear to people why. It appears to need be fixed in installation.
No one on any chat node has explained it, outside of mirroring the require.js boilerplate. The ment seems to continue as "do it this way and it works". The boilerplate does not cover all users.
Some failing lines include simple constructors like
moment(value)
, which is the whole point of the library.It appears the minor version upgrade from moment 2.9.0 to 2.10.0 may have forced deprecated code to break, at least for those using ECMAScript and a fallback. Reverting to 2.9.0 will allow you to keep working for now. If you only had the related breakage of moment.duration.fn disappearing, you can upgrade to 2.10.1 or above.