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 upcoming 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 upcoming ' +
'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 accomplish, then I can use it directly from global scope (window object in browser)
To get the current date and time, just call javascript moment() with no parameters like so: var now = moment(); This is essentially the same as calling moment(new Date()) . Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.
Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.
There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.
You 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.com/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.com/moment/moment/issues/1214), which is mostly philosophy.
The deprecation comes 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 comment 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With