Como configurar ESLint e Prettier 没有 Node?

2025-06-08

Como configurar ESLint e Prettier 没有 Node?

英文版可在 SourceLevel 上查看:https://sourcelevel.io/blog/how-to-setup-eslint-and-prettier-on-node

我建议您不要在ESLintPrettier看到这些内容。 O objetivo desse post é mais prático。

您可以通过使用 ESLint 和 Prettier 应用程序来预定义项目基础,并使用API 中的 hello world(世界各地)的必备条件来重新调整Netflix 框架,并使用其他方法来解决此问题。

您可以在项目中使用该项目,因为它是徽标的先决条件,因此您可以在以下方面进行配置:在应用程序节点上配置 ESLint?

先决条件

终端安装 com、bash、zsh 或 Fish。如果没有 Windows,请不要安装 bash,最好使用 powershell,因为它与您的命令兼容。请注意以下错误。

Node(10.16.3 或更高版本)和 npm(6.12.0 或更高版本)通过 Visual Studio Code (VSCode) 安装。

更改项目基础?

请注意,Abra o Terminal e crie uma Pasta com o nome de api-happy。继续使用mkdir意大利面和意大利面的命令:



mkdir api-happy


Enter fullscreen mode Exit fullscreen mode

Agora precisamos entrar na Pasta que acabamos de criar usando o comando cde, logo à frente, o nome da Pasta que queremos entrar:



cd api-happy


Enter fullscreen mode Exit fullscreen mode

展示! Agora que estamos dentro da Pasta já podemos iniciar or nosso projeto com node usando or npm (Node Package Manager).没有终端 usaremos 或 comando npm init -y, esse comando criará um arquivo chamado package.json com 项目基本信息:

  • 姓名(nome);
  • 版本(versão);
  • 描述(descrição);
  • 主要(arquivo que inicia o projeto);
  • 脚本(测试脚本和结尾脚本);
  • 关键词(palavras chaves para acharem seu projeto);
  • 作者(作者);
  • 许可证(licença)。

Agora que já sabemos o que o comando faz, 执行 ele noterminal, poravour (lembre-se que você tem que estar na Pasta do projeto):



npm init -y


Enter fullscreen mode Exit fullscreen mode

因此,您可以通过 VSCode 进行项目验证,并验证package.json是否成功。

O próximo passo,ainda dentro da pasta do projeto,é instalarmos uma dependentência ( restify ) no nosso projeto。 Para isso precisamos usar o npmusando o comando install:



npm install restify


Enter fullscreen mode Exit fullscreen mode

请注意,不要使用任何药物npm i restifynpm install restify您可以在--save包前面(不包括任何包restify)来保证包的安全性,因为它与 package.json 中的项目和信息无关,但这必需的。 Falando 没有package.json,请使用 VSCode 进行验证,以方便后续操作:



...
"dependencies": {
    "restify": "^8.4.0"
  }
...


Enter fullscreen mode Exit fullscreen mode

Ainda no seu VSCode com o seu projeto aberto e crie um arquivo chamado index.js e cole dentro dele o código a seguir:



const restify = require("restify");
const PORT = 5000
const app = restify.createServer()

app.get('/', (req, res) => 
  res.send({ hello: "World" }));

app.listen(PORT)


Enter fullscreen mode Exit fullscreen mode

与此相关的信息,请重新填写,并在 servidor com 上重新设置porta 5000 的 saberá 响应apenas a rota /com um simples jsonde {hello: "World"}。一些对代码感兴趣的部分,是为了进一步了解代码和一些关于代码的有趣的信息,主要是为了实现与ESLintPrettier 的配置相关的目的。

Por último só precisamos pedir para o node subir o nosso servidor com o comando a seguir noterminal:



node index.js


Enter fullscreen mode Exit fullscreen mode

您需要执行项目中的面食。

如何配置 ESLint 和 Node 应用程序?

在这个时间段内,我们将进行一些工作和工作,以及处理工作的质量和自动化。

伏特奥终端和德鲁贝奥服务 apertando 作为技术ctrlc军政府。 Agora sim、终端脚本和ESLint 配置。 ESLint 的配置需要使用 chamado .eslintrc.json进行配置(该配置可扩展为:js 和 yaml)。请使用手动操作手册,快速操作,快速使用或继续操作:



npx eslint --init


Enter fullscreen mode Exit fullscreen mode

npx 是什么?

NPX 安装为全局和临时文件,可用于删除或删除。我们将在全球范围内为您提供所需的空间和空间。 npx surgiu na versão 5.2 do npm。

在终端上执行命令并配置ESLint。 Vamos às perguntas。您可以自由地翻译和回复建议。请不要将所有说明作为可能的答复,否则您将无法获得有关评论的信息:

Pergunta 1:

How would you like to use ESLint? (Use arrow keys)

Como gostaria de usar 或 ESLint? (用作 teclas direcionais)

Resposta 1

To check syntax, find problems, and enforce code style

Checar sintaxe、buscar Problemas e forçar os padrão de código

Pergunta 2:

What type of modules does your project use?

美国项目的模块类型是什么?

回复 2:选择

JavaScript modules (import/export)

CommonJS (require/exports) 选项

None of these Nenhum desses 回复您

选择的选项,可以使用CommnsJS (require/exports)JavaScript 和蔗糖酶进行后端项目,可以选择选项JavaScript modules (import/export)

Pergunta 3:

Which framework does your project use?

美国项目的质量框架?

回复 3:

None of these

Nenhum desses

Pergunta 4:

Does your project use TypeScript?

Seu projeto usa TypeScript?

回复 4:
Escreva N e em seguida aperte a teclaENTER

Pergunta 5:

Where does your code run? (Press <space> to select, <a> to toggle all, <i> to invert selection)

如何计划并执行? (打开 <space> 进行选择,<a> 进行选择 tudo,<i> 进行逆变器选择)

答复 5:

Deixe apenas 进行选择Node

Pergunta 6:

How would you like to define a style for your project?

如何定义项目的代码?

Resposta 6:

Use a popular style guide

Usar um padrão de código 受欢迎

Pergunta 7:

Which style guide do you want to follow?
_Qual padrão de código você quer seguir?_

Resposta 7:

Airbnb (https://github.com/airbnb/javascript)

Pergunta 8:

What format do you want your config file to be in?

如何设置配置格式?

回复 8:

JSON

Pergunta 9:

Would you like to install them now with npm?

如何安装-los agora com npm?

回复 9:

Y

快点! Agora 的主题是ESLint criado 的配置信息。与本项目中的开发环境相关的安装功能的包。相关证书,请在 VSCode 中进行验证并验证.eslintrc.json并进行项目,从 abra 或 arquivo package.json中获取类似的后续内容:

.eslintrc.json



{
    "env": {
        "commonjs": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "airbnb-base"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018
    },
    "rules": {
    }
}


Enter fullscreen mode Exit fullscreen mode

包.json



{
  "name": "api-happy",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "restify": "^8.4.0"
  },
  "devDependencies": { 
    "eslint": "^6.5.1",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-import": "^2.18.2"
  }
}


Enter fullscreen mode Exit fullscreen mode

Agora abra 或 arquivo index.js。请注意编辑器提示的配置信息,但您将无法继续使用 AirBnB 的风格指南。请注意安装时需要使用 ESLint 插件作为配置命令,并告知错误信息。没有链接到在 VSCode 中安装 ESLint 插件的后续步骤:
https://marketplace.visualstudio.com/items ?itemName=dbaeumer.vscode-eslint

维西! Agora se você abrir or seu arquivo index.js vai se deparar com algo não muito agradável, um codigo cheio de sublinhados vermelhos, como na imagem a seguir:

ESLint 在 index.js 中的错误

如果鼠标位于const第 1 条中,请参阅 ESLint 错误描述,并符合以下要求:

大多数 ESLint 错误都出现在 index.js 中

require这是一个简单的错误,因此请按照以下说明进行操作restify。 Lembrar de fazer isso vai ser chato, muito chato, mas não se preocupe! É aqui que entra a magic do Prettier。 Agora vamos 是 Prettier 的配置部分,它负责对ESLint进行配置并实现作为编辑所需的更改。

感谢您的帮助,请参阅手册中的相关内容,以便了解有关 ESLint 或 ESLint 的算法。 Eu já fiz isso...kkk

Como configurar o Prettier com o ESLint?

Primeiro 需要安装Prettier插件,无需使用 VSCode,可以使用以下链接:https: //marketplace.visualstudio.com/items ?itemName=esbenp.prettier-vscode

Agora que já temos o Prettier 安装 VSCode Podemos 安装ESLint插件o Prettier se comunicar com o mesmo。打开终端 e,将面食制作成项目,执行命令并继续执行。安装所需配置所需的包:



npm i prettier eslint-config-prettier eslint-plugin-prettier


Enter fullscreen mode Exit fullscreen mode

如果您没有详细了解安装过程中的细节,那么您将无法在佩尔贡塔尔找到任何细节。
安装包时,请先安装.eslintrc.json,然后在没有 VSCode 和没有数组的情况下安装一个字符串extends"prettier"并遵循以下步骤:



...
"extends": [
  "airbnb-base",
  "prettier"
],
...


Enter fullscreen mode Exit fullscreen mode

A seguir da chave extendscrie a chave plugins(caso ela não exita), atribua um array para chave com a string, "prettier", conforme o código a seguir:



...
"extends": [
  "airbnb-base",
  "prettier"
],
"plugins": [
  "prettier"
]
...


Enter fullscreen mode Exit fullscreen mode

最后,在.eslintrc.json中配置一个更漂亮的 ESLint 功能,使其符合以下条件rules



...
  "rules": {
    "prettier/prettier": "error"
  }
...


Enter fullscreen mode Exit fullscreen mode

.eslintrc.json的最终结果



{
  "env": {
    "commonjs": true,
    "es6": true,
    "node": true
  },
  "extends": ["airbnb-base", "prettier"],
  "plugins": ["prettier"],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "ecmaVersion": 2018
  },
  "rules": {
    "prettier/prettier": "error"
  }
}


Enter fullscreen mode Exit fullscreen mode

打开index.js ,可以更改存档并删除存档。 Era para o prettier formatado seguindo os padrões e regras(AirBnB 风格指南)是由ESLint定义的。这是有关 VSCode 的详细信息的一个说明,它是目前为止所有的 VSCode 的格式。为了保证 VSCode 的配置功能不支持任何项目,请在 arquivo chamado settings.json 中使用 .vscode 和.vscode制作意大利面,谢谢大声喊叫 esse arquivo 和 essa 面食和 dentro do arquivo adicione 或 código a seguir:



{
  "editor.formatOnSave": true,
}


Enter fullscreen mode Exit fullscreen mode

启动索引.js并保存新的配置文件格式,自动进行配置。现在,您可以修复 ESLint 中的错误信息,并使其变得更漂亮。爱彼迎 (AirBnB) 的结论很简单,就是这样。想要变得更漂亮吗?请注意,如果您要重新使用 ESLint 定义,请不要将其重新定义为 próprias regras,请为插件页面添加漂亮的版本,而您更喜欢使用 aspas duplas 的插件:

Prettier 配置面板

如果您希望与 aspas simples 相关,可以使用 prettier.config.js 名称和 prettier.config.js 名称中的项目说明目标对象prettier.singleQuotefalsetrue



module.exports = {
  singleQuote: true
}


Enter fullscreen mode Exit fullscreen mode

Agora se você voltar ao seu arquivo index.js e salvá-lo, verá que ele será formatado para ficar com aspas simples.

Só mais um detalhe。您可以使用prettier.config.js来解决有关 AirBnB 与 ESLint 实用程序的兼容性问题的一些问题。我希望我的妈妈可以更漂亮,但与我们的妈妈的风格指南不同。建议您将阿尔古斯重新设计得更漂亮,以解决开发人员的长期问题,因为没有未来。请注意,将 arquivo prettier.config.js和 deixe ele com 作为以下配置:



module.exports = {
  tabWidth: 2,
  semi: true,
  singleQuote: true,
  trailingComma: 'es5',
};


Enter fullscreen mode Exit fullscreen mode

快点!因此,您需要使用 ESLint 来提供更漂亮的功能,以便使用 AirBnB 的风格指南。重要的是,要了解最终的风格指南,需要验证是否要更漂亮,请与我们的风格指南一起进行设计,然后将其配置为更漂亮。

Fico muito feliz em poder trocar conhecimento com você, obrigado port ter lido o post. Se tiver qualquer dúvida não deixe de perguntar。 Abraço bons estudos!

鏂囩珷鏉ユ簮锛�https://dev.to/collabcode/como-configurar-eslint-e-prettier-no-node-3kga
PREV
CSS 学习进度跟踪器和路线图概念模板
NEXT
新开发人员,你的简历太烂了