edrian
edrian

Reputation: 4531

Moment.js - "Accessing Moment through the global scope" warning message

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)

Upvotes: 5

Views: 8700

Answers (2)

Charles Merriam
Charles Merriam

Reputation: 20520

This really isn't an answer, but an appreciation of the problem:

  1. 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.

  2. The deprecation comes on usage, so its unclear to people why. It appears to need be fixed in installation.

  3. 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.

  4. Some failing lines include simple constructors like moment(value), which is the whole point of the library.

  5. 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.

Upvotes: 4

DoctorMick
DoctorMick

Reputation: 6793

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/

Upvotes: 10

Related Questions