Search code examples
javascripttypescripteslintaws-cdkcicd

Typescript eslint Parsing error: The keyword 'import' is reserved


I have a CDK application written in typescript. Locally when I run npm run eslint there are no errors.

But when the same thing is run in gitlab pipeline I get this error

  1:1  error  Parsing error: The keyword 'import' is reserved

here is my package.json

"devDependencies": {
    "@types/aws-lambda": "8.10.109",
    "@types/jest": "27.5.2",
    "@types/node": "^10.17.27",
    "@types/node-fetch": "^2.6.2",
    "@types/prettier": "2.6.0",
    "@types/yamljs": "0.2.31",
    "@typescript-eslint/eslint-plugin": "^4.30.0",
    "@typescript-eslint/parser": "^4.30.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.24.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "aws-cdk": "2.67.0",
    "esbuild": "^0.16.7",
    "eslint": "^7.32.0",
    "jest": "^27.5.1",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.3.2",
    "ts-jest": "27.1.4",
    "ts-node": "10.9.1",
    "typescript": "~3.9.7"
  },

npm script

 "eslint": "eslint --ext .ts,.tsx .",

.eslintrc.js

module.exports = {
  env: {
    es2021: true,
    node: true,
    jest: true,
  },
  extends: ["plugin:@typescript-eslint/recommended", "standard", "prettier"],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: 12,
    sourceType: "module",
  },
  plugins: ["@typescript-eslint"],
  rules: {
    "no-new": "off",
    camelcase: "off",
    "@typescript-eslint/no-non-null-assertion": "off",
    "@typescript-eslint/no-unused-vars": [
      "warn",
      { argsIgnorePattern: "^_.*" },
    ],
    "no-eq-null": "error",
  },
};

.gitlab-ci.yml

eslint:
  stage: static-test
  image:
    name: public.ecr.aws/docker/library/node:16-alpine
  script:
    - npm install
    - npm run eslint

What should I do to fix the error because locally it works. Should I change the name to .eslintrc instead of .eslintrc.js as many of the other issues have this problem.


Solution

  • To prevent this error from happening

    1:1  error  Parsing error: The keyword 'import' is reserved
    

    Change the name from .eslintrc.js to .eslintrc.cjs to indicate that you’re using CommonJS