Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: Intl is not defined in Node.js

I'm trying to construct a new Intl.Collator() object in Node.js.

Does anyone know why the Intl object wouldn't be present in a Node runtime?

According to MDN, it is specified as a Namespace by ECMAScript, so I don't see why it wouldn't be there.

like image 323
user2958725 Avatar asked Nov 21 '13 15:11

user2958725


3 Answers

Unfortunately node currently (as of version 0.10, at time of writing) does not support the ECMA-402 Intl object unless you perform a custom compile of node, which is documented in the node.js Readme.

With libicu i18n support:

svn checkout --force --revision 214189 \
   http://src.chromium.org/svn/trunk/deps/third_party/icu46 \
   deps/v8/third_party/icu46
./configure --with-icu-path=deps/v8/third_party/icu46/icu.gyp
make

make install

If compiling a custom build of node is not an option or the idea fills you with dread, a workaround is to use the intl module, a Javascript polyfill which covers much of the EMCA-402 standard, except for the Intl.Collator, the reasons for which are covered in the project Readme file.

Using the module is straight-forward:

npm install intl --save

Then in your node code:

var Intl = require('intl');
console.log(new Intl.NumberFormat("de-DE").format(12345678));

Hope this helps.

like image 181
Andrew Newdigate Avatar answered Nov 17 '22 23:11

Andrew Newdigate


Since io.js was merged into Node, it should be possible to use Intl in newer versions of Node (available in io.js from v3.1.0).

  • intl: Intl support using small-icu is now enabled by default in builds (Steven R. Loomis) #2264.
    • String#normalize() can now be used for unicode normalization.
    • The Intl object and various String and Number methods are present, but only support the English locale.
    • For support of all locales, node must be built with full-icu.

https://github.com/nodejs/node/blob/master/CHANGELOG.md#2015-08-18-version-310-fishrock123

like image 34
Jacob Avatar answered Nov 17 '22 23:11

Jacob


Node 0.12 has included support to Intl, but it comes with only a subset of ICU locales (i.e.: English). You need to build Node with flags for full ICU (or any subset you need). Long instructions for ICU build here: https://github.com/nodejs/node/wiki/Intl

I would recommend reading the FormatJS documentation: http://formatjs.io/

And especially the Intl Polyfill

https://github.com/andyearnshaw/Intl.js

var areIntlLocalesSupported = require('intl-locales-supported');

var localesMyAppSupports = [
    /* list locales here */
];

if (global.Intl) {
    // Determine if the built-in `Intl` has the locale data we need.
    if (!areIntlLocalesSupported(localesMyAppSupports)) {
        // `Intl` exists, but it doesn't have the data we need, so load the
        // polyfill and replace the constructors we need with the polyfill's.
        require('intl');
        Intl.NumberFormat   = IntlPolyfill.NumberFormat;
        Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
    }
} else {
    // No `Intl`, so use and load the polyfill.
    global.Intl = require('intl');
}

Intl.js does not (and will never) implement Intl.Collator. For this one, you really need to rely on Node 0.12 built with your required locales.

like image 1
noderman Avatar answered Nov 17 '22 22:11

noderman