Moment.js: Uncaught TypeError: Cannot read property 'defineLocale' of undefined at moment.js:13

Moment.js: Uncaught TypeError: Cannot read property 'defineLocale' of undefined at moment.js:13

当 运行 下面的小 html 文件时,我看到以下控制台日志错误:

moment.js:13 Uncaught TypeError: Cannot read property 'defineLocale' of undefined
    at moment.js:13
    at moment.js:9
    at moment.js:10

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>

    <script src="../scripts/libraries/moment.js"></script>

</head>

<body>


<script>
  var now = moment()
  console.log(now);
</script>

</body>
</html>

我也尝试用这个 CDN link 替换对本地库的引用:https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/af.js

有人知道这个错误是什么吗?

您的 moment.js 版本似乎有问题。您拥有的脚本仅适用于 locale,您需要包含 moment.js 脚本:

https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
</head>

<body>
  <script>
    var now = moment()
    console.log(typeof moment.defineLocale)
  </script>
</body>

</html>

当您在 moment js 之前包含语言环境 js 时,也会发生此错误。

例如以下给出错误:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/locale/tr.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

要更正,只需将上面的 moment js 放到语言环境 js 中即可。