Express Web Framework Tutorial : ESLint script

Hi I’m having a problem the Express Web Framework tutorial.

In the “Setting up a Node development environment”, we are showed how to add ESLint as a devdependencies. So we need to update package.json in the scripts part. Can you give us a concrete example, because

  • adding “eslint src/js” is not correct. I created the directory “src/” in my myapp folder then put index.js
  • then running npm run lint, I got the error that eslint is missing a configuration file.

I cannot run eslint --init as it seems that bash does not recognize eslint even if my npm install of eslint with --save-dev worked.

I really don’t know what to do.

Would it be better to install ESlint generally then declare it in “devdepencies” so maybe I can at least run the ESlint --init configuration ?

I’ve done the general installation with “npm install eslint -g” and the config with “eslint --init”, setting among others that I’ll be testing Javascript modules but I got this error :

error ‘require’ is not defined

I was testing the index.js where I had to include the http package !

I’m really lost here !

Hi there!

Can you give me the URL of the bit of the tutorial you are stuck on?

Hi Chris,

Thanks for trying to help !

This is the URL of my tutorial :

I was following the section “Using NPM”, at the “Running tasks” step.
I successfully ran through all the steps above when I encountered the problems I described.

Right.

So the good news is that you don’t need to follow this bit of the tutorial to use the rest of it. I’d probably advise ignoring eslint and npm scripts for now, as they are fiddly and hard to configure.

If you want to continue trying to get this working, here are some tips:

  1. You need to add in the npm script to the package.json file, something like this:
{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "eslint": "^6.0.1"
  }
}
  1. To create an eslint config, the easiest way is to use npx eslint --init - npx directly runs commands that are available inside node modules, rather than you having to go through the nightmare of figuring out all the path issues and stuff

  2. You can then run eslint on your index file using npx eslint index.js. This works fine

  3. To run the actual npm script we added to the package.json file, you should be able to do npm run lint. This gives the expected result, but it also throws an error right afterwards. And I’m not sure why.

I’ve run out of time to look into this any more, but I hope this helps a bit.

Thanks again for looking at my problem.

I understood that it eslint is not needed to resume the tutorial and I did what showed with your package.json.

I didn’t know about npx as I’m supposed to learn the essentials and having the minimal working steps to reach some knowledge !

What troubles me the most is that eslint on the the index.js file ends up saying that require() is not defined !

Having followed other node.js and javascript tutorial, I would have use “import”/“export” to include other .js file in ES6. But I’ve noticed that node would not recognize that syntax and I should use require().

Now, the eslint package does not recognize require, it is quite shocking !
It is already confusing elsewhere, I did not expect more confusing here at MDN !

Thanks again Chris, I’m still following the tutorial.