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

Viewed 2729

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?

3 Answers

Alternatively to creating a separate .eslintrc file, you can also add it to the package.json file.

The docs state:

To specify environments in a configuration file, use the env key and specify which environments you want to enable by setting each to true. For example, the following enables the browser and Node.js environments

{
    "name": "mypackage",
    "version": "0.0.1",
    "eslintConfig": {
        "env": {
            "browser": true,
            "node": true
        }
    }
}
Related