Nodejs+Typescript+Eslint+Prettier+Husky项目构建

仓库地址

准备工作

确保已经安装了git以及Node.js和npm,通过git -vnode -vnpm -v检查是否安装。

初始化项目

# 进入你的目录
cd your-directory

# 初始化 npm 项目
npm init -y

# 安装 TypeScript 和 ts-node
npm install --save-dev typescript ts-node

# 初始化 TypeScript 配置
npx tsc --init

# 创建 src 目录
mkdir src

# 创建你的 TypeScript 文件
touch src/index.ts

在新建的tsconfig.json中,修改target、module、和outDir,其中outDir设置为./dist,其他根据情况设置。

Eslint安装和配置

# 安装 ESLint
npm install --save-dev eslint

# 初始化 ESLint 配置
npx eslint --init

根据情况选择配置:

You can also run this command directly using 'npm init @eslint/config'.
Need to install the following packages:
  @eslint/create-config@0.4.6
Ok to proceed? (y) y
✔ How would you like to use ESLint? · style
✔ What type of modules does your project use? · commonjs
✔ Which framework does your project use? · none
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · node
✔ How would you like to define a style for your project? · guide
✔ Which style guide do you want to follow? · standard-with-typescript
✔ What format do you want your config file to be in? · JavaScript
Checking peerDependencies of eslint-config-standard-with-typescript@latest
The config that you've selected requires the following dependencies:

eslint-config-standard-with-typescript@latest @typescript-eslint/eslint-plugin@^6.1.0 eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 || ^16.0.0  eslint-plugin-promise@^6.0.0 typescript@*
✔ Would you like to install them now? · No / Yes
✔ Which package manager do you want to use? · npm
Installing eslint-config-standard-with-typescript@latest, @typescript-eslint/eslint-plugin@^6.1.0, eslint@^8.0.1, eslint-plugin-import@^2.25.2, eslint-plugin-n@^15.0.0 || ^16.0.0 , eslint-plugin-promise@^6.0.0, typescript@*

added 113 packages in 5s

Prettier安装和配置

  1. 首先,安装 Prettier,在项目目录中,运行
npm install --save-dev prettier
  1. 创建一个 .prettierrc 文件来配置 Prettier。你可以在这个文件中设置你的格式化选项,例如:
{
	"semi": true,
	"singleQuote": true,
	"tabWidth": 4
}

在Eslint中使用Prettier插件

安装 eslint-plugin-prettier 和 eslint-config-prettier

npm install --save-dev eslint-plugin-prettier eslint-config-prettier

编辑.eslintrc.js文件

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es2022": true
    },
    "extends": ["standard-with-typescript","prettier"],
    "plugins": ["prettier"],
    "overrides": [
        {
            "env": {
                "node": true
            },
            "files": [
                ".eslintrc.{js,cjs}"
            ],
            "parserOptions": {
                "sourceType": "script"
            }
        }
    ],
    "parserOptions": {
        "ecmaVersion": "latest",
        "project": "./tsconfig.json"
    },
    "rules": {
        "prettier/prettier": "error"
    }
}

Husky安装和配置

# 安装 Husky和commitlint 
npm install --save-dev @commitlint/cli @commitlint/config-conventional husky

# 创建git仓库
git init

# 可修改分支名
git branch -m 旧名字 新名字

# 初始化 Husky
npx husky install

创建一个名为 commitlint.config.js 的文件,内容如下:

module.exports = {extends: ['@commitlint/config-conventional']};

提交前检查代码风格和提交格式:

npx husky add .husky/pre-commit "npm run lint"
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

修改tsconfig.json

可根据需要灵活配置

启用@表示src目录

tsconfig.json需添加设置

{
    ...
    "baseUrl": "./",  
    "paths": {
      "@/*": ["src/*"]
    }, 
    ...
}

执行npm install tsconfig-paths

修改package.json

将scripts修改为

"scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "ts-node -r tsconfig-paths/register src/index.ts",
    "debug": "node --inspect-brk dist/index.js",
    "lint": "eslint ./src --ext .ts --fix",
    "format": "prettier --write ./src",
    "prepare": "husky install"
},

设置vscode调试

可参考以下的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/src/index.ts",
            "sourceMaps": true,
            "runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
            "outFiles": [
                "${workspaceFolder}/**/*.js"
            ],
            "console": "integratedTerminal"
        }
    ]
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载Vue Node.js MySQL项目,您需要按照以下步骤进行操作: 1. 首先,确保您已经安装了Node.js和MySQL数据库。如果没有,请前往它们的官方网站并按照说明进行安装。 2. 打开您的命令行终端,创建一个新的项目文件夹。您可以使用以下命令创建一个名为"my-project"的文件夹: ``` mkdir my-project ``` 3. 进入新创建的项目文件夹: ``` cd my-project ``` 4. 使用以下命令初始化一个新的Node.js项目: ``` npm init -y ``` 这将会自动生成一个默认的`package.json`文件。 5. 安装Vue.js。使用以下命令将Vue.js添加到您的项目中: ``` npm install vue ``` 6. 安装Express框架,用于构建服务器端应用。使用以下命令将Express添加到您的项目中: ``` npm install express ``` 7. 安装mysql模块,用于连接和操作MySQL数据库。使用以下命令将mysql模块添加到您的项目中: ``` npm install mysql ``` 8. 创建一个新的JavaScript文件,例如`app.js`,并打开它。 9. 在`app.js`中编写您的服务器端代码,包括Vue.js的前端代码和与MySQL数据库的交互。您可以根据您的项目需求编写自定义的代码。 10. 保存`app.js`文件并返回终端。 11. 在终端中运行以下命令以启动服务器: ``` node app.js ``` 12. 当服务器启动成功后,您将看到一个成功的消息。此时,您可以在浏览器中访问`http://localhost:3000`来查看您的Vue Node.js MySQL项目。 总结一下,下载Vue Node.js MySQL项目的步骤包括:创建项目文件夹,初始化Node.js项目,安装Vue.js、Express和mysql模块,编写服务器端代码,启动服务器并在浏览器中查看项目。这样,您就可以成功下载和运行Vue Node.js MySQL项目了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值