最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

When running the small html file below I see the following console log error:

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>

When running the small html file below I see the following console log error:

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>

I have also tried replacing the reference to the local library with this CDN link: https://cdnjs.cloudflare./ajax/libs/moment.js/2.18.1/locale/af.js

Anyone know what this error is?

Share Improve this question asked Jun 17, 2017 at 0:48 Sean DSean D 4,29213 gold badges52 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 24

It seems to be a problem with your version of moment.js. The script that you have is only for locale, you need to include the moment.js script:

https://cdnjs.cloudflare./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./ajax/libs/moment.js/2.18.1/moment.js"></script>
</head>

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

</html>

This error also occurs when you include locale js prior to moment js.

For instance following gives error:

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

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

To correct, just put moment js above to locale js.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论