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

Sea*_*son 4 macos node.js visual-studio-code

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...');
})
Run Code Online (Sandbox Code Playgroud)

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?

Mat*_*att 8

聚会迟到了,但遇到了同样的问题。对我来说,解决方法是添加节点类型,以便 VSCode 识别它们:

转到终端视图,然后输入以下内容: npm install @types/node --save-dev

然后点击Cmd + Shift + P(至少对于 Mac)打开 VSCode 命令搜索对话框,然后选择Reload Window.

当它再次出现时,该错误和任何其他与无法识别的 Node.JS 内容相关的内容都应该消失。


Dan*_*mms 6

您收到的警告来自eslint 扩展名。虽然它可能在 Node.JS 中有效,但它会警告您,__dirname因为它并非在所有 JavaScript 环境中都有效,例如在浏览器中。

要取消警告,您需要.eslintrc直接在项目的根目录中创建一个文件,并指示代码将在节点中运行,如下所示:

{
    "env": {
        "node": true
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在.eslint此处查看用于为实际 vscode 代码库配置 eslint的文件https://github.com/microsoft/vscode/blob/main/.eslintrc.json