简体   繁体   中英

Ignoring eslint error for moment.js

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
...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM