[英]Code formatting with prettier is not working in nest js
I am using Visual Studio Code.我正在使用 Visual Studio 代码。 In my Nest JS project, the code is not formatting according to prettier rules.
在我的 Nest JS 项目中,代码没有根据更漂亮的规则进行格式化。 I already set
.prettierrc
and .eslintrc
.我已经设置
.prettierrc
和.eslintrc
。 Also i have set formatOnSave: true
from the settings.json
file.我还从
settings.json
文件中设置了formatOnSave: true
。
Portion of my settings.json file我的settings.json文件的一部分
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
My .eslintrc file -我的.eslintrc文件 -
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off'
},
};
And finally my .prettierrc file -最后是我的.prettierrc文件 -
{
"useTabs": true,
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"importOrder": ["^[./]"],
"importOrderSortSpecifiers": true,
"importOrderSeparation": true
}
Can you please tell me what to do to format the code properly?你能告诉我如何正确格式化代码吗?
In my case the.prettierrc file was not in the root of the project and VS Code couldn't find it.在我的情况下,.prettierrc 文件不在项目的根目录中,VS Code 找不到它。 (I just have to change the path to this file or move it to the root of the project)
(我只需要更改此文件的路径或将其移动到项目的根目录)
One reason prettier will skip formatting a file is when the file has syntax error(s) in it. prettier 会跳过格式化文件的一个原因是文件中存在语法错误。 If prettier can't parse it, it errs on the side of caution and doesn't touch it.
如果 prettier 无法解析它,它会在谨慎方面犯错并且不会触及它。
For me the issue was my file's language was actually "[typescriptreact]", had to include that as one of the languages it's defaultFormatter for对我来说,问题是我的文件的语言实际上是“[typescriptreact]”,必须将其作为其中一种语言包括在内,它是 defaultFormatter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.