1

I am adding moment.js in my react app as an external resource like this:

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

When I use moment in my code file where eslint is enabled then I am getting this eslint error: 'moment' is not defined(no-undef).

This is my .jsx file-

constructor() {
  super()
  this.state = {
    startDate: moment()
  }
}
...

My eslintrc.yml look like this:

extends:
- jquery
- airbnb

parser: babel-eslint

parserOptions:
  ecmaVersion: 6
  ecmaFeatures:
    experimentalObjectRestSpread: true
    jsx: true
  sourceType: "module"

env:
  browser: true
  node: true
  jquery: true
...
sahil solanki
  • 507
  • 6
  • 20
  • 2
    As you are using moment globally (bound to the window object) you can use window.moment in your modules. As an alternative, you can install moment by npm and use it as any other module in your code. – Hoshts Sep 16 '17 at 20:37

2 Answers2

3

If you use webpack. npm install moment --save import moment from moment;

Or you can disable linting by adding a comment after moment() in the same line. // eslint-disable-line

Rafael Hovsepyan
  • 781
  • 8
  • 14
0

If using is a hard requirement

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

But you can edit the .eslintrc file, you can tell eslint that you have made moment available in the global namespace, which is what your script tag is doing.

{
//...
    "env": {
        "moment": true
    }
//...
}

Link to eslint environment docs