Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - Node.js - Cannot find name '__dirname'

Just to preface I'm not a professional coder, but nonetheless I got roped into building a website for my boss after he found out I took "Web Design" in high school 10 years ago. Back then static sites were fine, and heck CSS was just starting to show it's potential, but I digress.

I'm working on a Node.js project on Visual Studio Code right now and have a weird exception. The code works fine, but I guess it's just curiosity at this point. Anyway, here's my code.

app.js

var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
multer = require('multer'),
controller = require('./controllers');

//Sets the view engine to jade.
app.set('views', __dirname + '/frontend');
app.set('view engine', 'jade');

//sets up the development enviroment
if (app.get('env') === 'development') {
  app.locals.pretty = true;
  app.use(express.static(__dirname + '/build/public'))
  var build = require(__dirname + '/build.js');
  app.use('css/*', function(req, res, next){
    build.style();
    next();
  });
}

//Sets up the public directory to serve static files - This will be depricated quite soon...
//app.use(express.static(__dirname + '/public'));

//Initalizes site-wide local variables
//app.set('title', 'Halvorson Homes');

//Sets up body-parser to be able to read RESTful requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(multer({dest: 'tmp'}).fields());

//Load Controllers
app.use( '/' , controller);


//makes the app listen on port 3000
app.listen(3000, function() {
  console.log('Listening on port 3000...');
})

My folder structure is pretty much as follows

  • controllers
    • index.js
    • designs.js
  • models
    • designs.js
  • frontend
    • common
      • layout.jade
      • header.jade
      • footer.jade
      • client.js
      • style.less
    • index
      • index.jade
      • client.js
      • style.less
    • designs
      • index.jade
      • client.js
      • style.less
  • build
    • tmp
    • srv
  • app.js
  • build.js
  • package.json

According to VS Code's built in debugger there's exceptions on lines 8, 14, and 15. They're the only places I used __dirname in the entire project. This is an annoying to me, as I am too much of a perfectionist. Is it Node, VS Code, or something else?

like image 908
Sean Halvorson Avatar asked Feb 08 '23 11:02

Sean Halvorson


1 Answers

The warning you are getting is from the eslint extension. While it may be valid in Node.JS, it's warning you about __dirname because it's not valid in all JavaScript environments such as in browsers.

To suppress the warning you will want to create an .eslintrc file in your project's root directly and indicate that the code will be running in node like so:

{
    "env": {
        "node": true
    }
}

You can see the .eslint file used to configure eslint for the actual vscode codebase here https://github.com/microsoft/vscode/blob/main/.eslintrc.json

like image 70
Daniel Imms Avatar answered Feb 16 '23 03:02

Daniel Imms