ReferenceError:Intl未在Node.js中定义 [英] ReferenceError: Intl is not defined in Node.js

查看:117
本文介绍了ReferenceError:Intl未在Node.js中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Node.js中构建一个新的Intl.Collat​​or()对象。

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

有谁知道为什么 Intl 对象不会出现在Node运行时中?

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

根据MDN ,它被ECMAScript指定为命名空间,所以我不明白为什么它不会'在那里。

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

推荐答案

不幸的是,节点当前(截至编写时的0.10版本)不支持ECMA-402 Intl 对象,除非您执行节点的自定义编译,node.js中记录了自述文件

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.


支持libicu i18n:

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

make install

如果编译节点的自定义构建不是一个选项,或者这个想法会让你感到恐惧,解决方法是使用 intl模块 ,这是一个涵盖的Javascript polyfill除了 Intl.Collat​​or 之外,EMCA-402标准的大部分内容都包含在项目自述文件中。

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.

使用该模块很简单:

npm install intl --save

然后在您的节点代码中:

Then in your node code:

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

希望这有帮助。

这篇关于ReferenceError:Intl未在Node.js中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆