The problem
When trying to use import
or export
statements in TypeScript (*.ts
) files eslint
complains:
Import and export declarations are not supported yet.
eslint(node/no-unsupported-features/es-syntax)
Solution
Add following rule config to your eslint
settings:
//.eslintrc.js
module.exports = {
...
rules: {
"node/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
],
},
...
}
Notes
My full eslint
configuration for reference:
//.eslintrc.js
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2018,
sourceType: "module",
},
plugins: ["node", "unicorn", "prettier", "@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:node/recommended",
"plugin:unicorn/recommended",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
env: {
node: true,
},
rules: {
"no-console": "error",
"unicorn/no-array-reduce": "off",
"prettier/prettier": "error",
"@typescript-eslint/no-var-requires": "off",
"node/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
],
},
settings: {
node: {
tryExtensions: [".js", ".json", ".node", ".ts"],
},
},
};
This only works when I have following settings in package.json
:
//package.json
{
...
"type": "commonjs",
...
}
References
Photo by Brett Jordan on Unsplash
Top comments (4)
Hello! I want you to ask about unicorn plugin. What for we need to use it?
Hi Sergei unicorn is popular eslint plugin that adds some extra formatting rules. You don't need it at all. I attached the full config only because I think setting up all the
extends
/plugins
is very confusing and this should give the reader ability to have copy&paste solution. If you know what you're doing, adding the extrarules
option is a way to go.Michal thanks! I google it but wasn't sure. Actually i'm starting to use airbnb's rules in my project and have never heard about unicorn before. And I'm wondering if anyone has compared them?
Not sure tbh. Quick look at the Airbnb one it seems that: