我试图设置一个简单的类型记录,节点环境的绝对路径,而不是相对路径。
我跟踪了视频这里:但在运行npm run start:dev时无法正确解析绝对导入。VSCode intellisense能够很好地解决绝对导入路径,但是当我编译时,我会得到编译错误。
注意:相关路径正在编译并正常工作。但是绝对路径会产生编译错误。
这是我的.nvmrc
v16.15.1下面是我的代码结构:

以下是我的简单代码:
src/index.ts:
import add from '@src/math/add';
console.log(add(2, 1));这里,@src/math/add给出了编译错误。./math/add编译得很好。
src/math/add.ts:
import force from '@src/science/physics';
const add = (a: number, b: number): number => {
console.log(`force:${force(5, 3)}`);
return a + b;
};
export default add;这里,@src/science/physics给出了编译错误。../science/physics编译得很好。
src/physics/force.ts:
const force = (mass: number, accelaration: number): number => mass * accelaration;
export default force;这是我的tsconfig.json
{
"ts-node": {
"require": ["tsconfig-paths/register"],
"esm": true,
"experimentalSpecifierResolution": "node"
},
"exclude": ["node_modules", "dist", "coverage"],
"include": ["src"],
"compilerOptions": {
"target": "ES2020",
"lib": ["DOM", "ESNext"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
/* JavaScript Support */
"allowJs": true,
/* Emit */
"outDir": "dist",
"removeComments": true,
/* Type Checking */
"strict": true,
"noImplicitAny": true,
/* Modules */
"module": "ES2020",
"moduleResolution": "node",
"rootDir": "." /*meaning wherever is this tsconfig.json file, that is the root directory*/,
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"]
},
/* Interop Constraints */
"isolatedModules": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
/* Completeness */
"skipLibCheck": true
}
}这是我的package.json
{
"type": "module",
"name": "express-proj-setup-tut",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"start:dev": "ts-node -r tsconfig-paths/register ./src/index.ts",
"start:prod": "node -r ts-node/register/transpile-only -r tsconfig-paths/register ./dist/src/index.ts",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.7.9",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"eslint": "^8.22.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.4.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0",
"typescript": "^4.7.4"
}
}最后,下面是终端中的错误消息:

如果有人能帮我找到绝对可行的道路,我会非常感激的。
谢谢
发布于 2022-08-22 18:02:41
我无法解决您在package.json中配置模块和在tsconfig.json中配置模块的问题,但是如果您从package.json中删除"type": "module"并在tsconfig.json中更改为"module": "CommonJS",您将能够运行"start:dev": "ts-node -r tsconfig-paths/register src/index.ts"。
对我来说输出是
force:15
3发布于 2022-08-22 16:00:49
检查您的node.js版本是否高于14.6.0
有带有ts代码的./src文件夹,./dist文件夹作为tsc输出文件夹,按照以下方式将./src映射为#root全局路径:
// tsconfig.json
{
"compilerOptions": {
...
"baseUrl": "./src",
"paths": {
"#root/*": [
"./*"
]
}
}
}// package.json
{
...
"imports": {
"#root/*": "./dist/*"
},
}这样的别名应该以#符号开头。
有用的链接:
发布于 2022-08-22 16:32:29
在你的tsconfig.json中,你需要改变
"paths": {
"@src/*": ["src/*"]
},为此:
"paths": {
"@/*": [
"src/*"
]
},在include中,您需要添加包含ts文件的所有路径,如下所示:
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],我希望解决这个编译错误:-)
https://stackoverflow.com/questions/73447818
复制相似问题