弄了半天终于弄好了 node配置@ 然后看下怎么再node中配置@路径
首先再node中初始化了tsconfig.json 然后配置给了
//配置这两个
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
"@/*": ["./src/*"]
},
//完整的tsconfig.json
{
"compilerOptions": {
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
"@/*": ["./src/*"]
},
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
}
}
然后会发现报错: 报错内容
Error: Cannot find module '@/global'
Require stack:
- C:\Users\Administrator\Desktop\hnxd_node\src\admin\login\index.js
- C:\Users\Administrator\Desktop\hnxd_node\src\main.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (C:\Users\Administrator\Desktop\hnxd_node\node_modules\@cspotcode\source-map-support\source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:922:27)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object.<anonymous> (C:\Users\Administrator\Desktop\hnxd_node\src\admin\login\index.js:41:18)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Object.require.extensions.<computed> [as .js] (C:\Users\Administrator\Desktop\hnxd_node\node_modules\ts-node\src\index.ts:1608:43)
at Module.load (node:internal/modules/cjs/loader:1119:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\Administrator\\Desktop\\hnxd_node\\src\\admin\\login\\index.js',
'C:\\Users\\Administrator\\Desktop\\hnxd_node\\src\\main.ts'
]
}
这时候能 需要配合
module-alias
安装:npm install module-alias
后再package.json
中配置
"_moduleAliases": {
"@": "./src"
}
以下是完整的package.json
{
"name": "xxxx_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --exec ts-node src/main.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-jwt": "^8.4.1",
"log4js": "^6.9.1",
"module-alias": "^2.2.3",
"mysql2": "^3.9.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"@types/express": "^4.17.21"
},
//配置的@
"_moduleAliases": {
"@": "./src"
}
}
重要!重要!重要!
配置完后,一定要在入口文件中初始化下,否则还是报上面哪个错误
本人的入口文件是main.ts
// 一定要加这句话
import 'module-alias/register';
然后就可以用了 和 vue、react中的@一样 我这里@配置在了src下 根据各位的需求来
这是我的路径: