简体   繁体   English

Visual Studio Code - Node.js - 找不到名称“__dirname”

[英]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.只是作为序言,我不是一个专业的编码员,但尽管如此,在我的老板发现我 10 年前在高中学习了“网页设计”后,我还是被迫为我建立了一个网站。 Back then static sites were fine, and heck CSS was just starting to show it's potential, but I digress.那时静态网站还不错,而且 CSS 刚刚开始展示它的潜力,但我离题了。

I'm working on a Node.js project on Visual Studio Code right now and have a weird exception.我现在正在 Visual Studio Code 上处理 Node.js 项目,但有一个奇怪的例外。 The code works fine, but I guess it's just curiosity at this point.代码运行良好,但我想这只是出于好奇。 Anyway, here's my code.无论如何,这是我的代码。

app.js应用程序.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索引.js
    • designs.js设计.js
  • models模型
    • designs.js设计.js
  • frontend前端
    • common常见的
      • layout.jade layout.jade
      • header.jade header.jade
      • footer.jade页脚.玉
      • client.js客户端.js
      • style.less无风格
    • index索引
      • index.jade索引.jade
      • client.js客户端.js
      • style.less无风格
    • designs设计
      • index.jade索引.jade
      • client.js客户端.js
      • style.less无风格
  • build建造
    • tmp时间
    • srv服务端
  • app.js应用程序.js
  • build.js构建.js
  • package.json包.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.根据 VS Code 的内置调试器,第 8、14 和 15 行有例外。它们是我在整个项目中唯一使用 __dirname 的地方。 This is an annoying to me, as I am too much of a perfectionist.这对我来说很烦人,因为我太完美主义者了。 Is it Node, VS Code, or something else?是 Node、VS Code 还是其他什么?

The warning you are getting is from the eslint extension .您收到的警告来自eslint 扩展名 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.虽然它可能在 Node.JS 中有效,但它会警告您有关__dirname因为它并非在所有 JavaScript 环境(例如浏览器)中都有效。

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

{
    "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您可以在.eslint查看用于为实际 vscode 代码库配置 eslint 的.eslint文件https://github.com/microsoft/vscode/blob/main/.eslintrc.json

Late to the party, but just had the same issue.聚会迟到了,但刚刚遇到了同样的问题。 The fix for me was to add the node types so VSCode recognises them:我的解决方法是添加节点类型,以便 VSCode 识别它们:

Go to the Terminal view, then enter the following: npm install @types/node --save-dev转到终端视图,然后输入以下内容: npm install @types/node --save-dev

Then hit Cmd + Shift + P (for Mac at least) to bring up the VSCode command search dialogue, then select Reload Window .然后点击Cmd + Shift + P (至少对于 Mac)以打开 VSCode 命令搜索对话框,然后选择Reload Window

When it reappears, that error and any other related to Node.JS stuff not being recognised should be gone.当它再次出现时,该错误以及与 Node.JS 相关的任何其他内容都不会被识别。

Alternatively to creating a separate .eslintrc file, you can also add it to the package.json file. .eslintrc创建单独的.eslintrc文件,您还可以将其添加到package.json文件中。

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.要在配置文件中指定环境,请使用 env 键并通过将 each 设置为 true 来指定要启用的环境。 For example, the following enables the browser and Node.js environments例如,以下启用浏览器和 Node.js 环境

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM