
ESLint can be used to identify syntax errors in your Node.js code. Almost always, you will already have a Node.js app that is using one or more modules. For example, perhaps you have an Express app. For more details on Express, check out my article FreeKB - Node.js - Getting Started with Node.js Express. Let's say app.js has the following. Notice this includes import("express") which means you would have already installed the Express package.
from express import "express";
const app = express();
app.get('/', (req, res) => {
console.log("Hello Console");
res.send("Hello World");
})
app.listen(12345);
console.log("Node.js Express app is up and running");
The npm list command can be used to see if you have the Express package installed in the node_modules directory in your present working directory.
~]$ npm list
foo@ /tmp/demo
└── express@4.19.2
Or the npm list --global command can be used to see if you have the Express package installed in the node_modules directory in your default installation of Node.js.
~]$ npm list --global
/usr/local/nodejs/node-v20.10.0-linux-x64/lib
└── npm@10.2.3
If you have the Express package installed in your present working directory, then you should also have a package.json file in your present working directory.
]$ ls -l
-rw-r--r-- 1 john.doe john.doe 219 Jul 31 20:08 app.js
drwxr-xr-x 66 john.doe john.doe 1340 Jul 31 20:06 node_modules
-rw-r--r-- 1 john.doe john.doe 53 Jul 31 20:06 package.json
-rw-r--r-- 1 john.doe john.doe 25388 Jul 31 20:06 package-lock.json
And the package.json file should contain Express.
~]$ cat package.json
{
"type": "module",
"dependencies": {
"express": "^4.19.2"
}
}
Let's use the npm install command to install the eslint and @eslint/js package.
npm install eslint @eslint/js
Now the npm list command should show that the eslint and @eslint/js packages are installed in the node_modules directory in your present working directory.
]$ npm list
foo@ /tmp/demo
├── @eslint/js@9.8.0
├── eslint@9.8.0
└── express@4.19.2
And package.json should also include eslint and @eslint/js.
]$ cat package.json
{
"type": "module",
"dependencies": {
"@eslint/js": "^9.8.0",
"eslint": "^9.8.0",
"express": "^4.19.2"
}
}
Next let's create the eslint.config.js file in the same directory as your app and package.json file, your present working directory.
touch eslint.config.js
There are many ways to write the eslint.config.js file. Here's a simple starter example. Let's add this to the eslint.config.js file. In this example, if app.js has unsed variables or undefined variables or constants, a warning will be returned.
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: {
"no-unused-vars": "warn",
"no-undef": "warn"
}
}
];
And let's use eslint to see if there are any syntax errors in app.js. There is!
~]$ node_modules/eslint/bin/eslint.js app.js
/tmp/demo/app.js
1:6 error Parsing error: Unexpected token express
✖ 1 problem (1 error, 0 warnings)
Because app.js mistakenly has from express import "express" but should have import express from "express".
from express import "express";
Let's correct app.js to have
impress express from "express";
Now eslint returns warnings instead of errors. Getting better.
~]$ node_modules/eslint/bin/eslint.js app.js
/tmp/demo/app.js
5:3 warning 'console' is not defined no-undef
10:1 warning 'console' is not defined no-undef
✖ 2 problems (0 errors, 2 warnings)
Did you find this article helpful?
If so, consider buying me a coffee over at