'Why does ESLint throw an error while using export/import statement on Node.js 12.13.0?

I have a project built on Node.js, DynamoDB and other AWS services, deployed on Serverless architecture. I also have the ESLint package installed.

I am getting the following error:

ESLint: Import and export declarations are not supported yet on Node 8.0.0. (node/no-unsupported-features)

Following are the details of my project:

Node version: 12.13.0
NPM version: 6.12.0
Serverless version: 1.40.0
ESLint: 6.8.0

I have double verified by node version of my project and my local. Both are the same (12.13.0).

I am able to use async/await but whenever I try using import/export, it gives me the error.

Following is my .eslintrc file :

{
  "extends" : [
    "eslint:recommended",
    "plugin:node/recommended"
  ],
  "plugins": [
    "promise",
    "node"
  ],
  "env"     : {
    "browser" : false,
    "node": true,
    "es6": true
  },
  "parserOptions": {
    "ecmaVersion": 9,
    "sourceType": "module",
    "impliedStrict": false
  },
  "globals" : {
  },
  "rules": {
    "no-console": 0,
    "no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }],
    "node/no-unpublished-require": [
      "error",
      {
        "allowModules": [
          "aws-sdk"
        ]
      }
    ],
    "node/no-unsupported-features": ["error", {
      "version": 8,
      "ignores": []
    }]
  }
}
`


Solution 1:[1]

Node.js by default uses Common.js style for import/export. In the new versions of Node.js you can use different extensions or the --experimental-modules option in the command line.

*.mjs you use ES6 import/export

*.js or *.cjs you use commonjs

If you consider that ECMAScript modules import/export are still experimental, I would say that ESLint is quite correct.

You might want to override EsLint rules in order to make it work with that. Firstly consider that the rule you mentioned is obsolete so you might want to use the new ones => https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features.md

Because those rules are obsolete I would suggest you to take a look at your eslint and its external rulesets version (if you use them) and try again.

In any case...

Check/create the .eslintrc.json file in the root of your project and override the project rules you might want to change.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1