我向我的TypeScript项目添加了一个路径别名,以使导入更易于管理,但是,在运行已编译的JavaScript时会出现一个错误。
当我运行时:node ./bin/index.js
答复是:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '@castorApp/CastorApp'
我进口的方式是:
import { CastorApp } from "@castorApp/CastorApp";
我的文件夹结构是:
CastorJS/
├─ node_modules/
├─ bin/
├─ src/
│ ├─ castorApp/
│ │ ├─ CastorApp.ts
│ │ ├─ CommandService.ts
│ ├─ types/
│ ├─ index.ts
├─ .gitignore
├─ package-lock.json
├─ package.json
├─ tsconfig.json
我的tsconfig.json
看起来是这样的:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"lib": [
"es2015"
],
"moduleResolution": "node",
"sourceMap": true,
"outDir": "bin",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
],
"@castorApp*": [
"src/castorApp",
"src/castorApp*"
]
},
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": [
"src/**/*"
]
}
我的package.json
看起来是这样的:
{
"name": "castor",
"version": "1.0.0",
"description": "TypeScript version of Castor",
"main": "bin/index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ZtModArchive",
"license": "MIT",
"dependencies": {
"@types/node": "^18.7.18"
},
"devDependencies": {
"typescript": "^4.8.3"
}
}
我在博客上看到有人推荐module-alias
包,但是这个包在自述文件中有以下警告:
警告:此模块不应用于其他npm模块,因为它会修改默认的需求行为!它被设计用于开发最终项目,如网站、应用程序等。
因此,由于我确实希望将我的代码作为一个包发布,因此不幸的是,这不是一个选项。
发布于 2022-09-20 10:22:14
在您的tsconfig.json中尝试如下:
"@castorApp/*": ["src/castorApp/*"]
https://stackoverflow.com/questions/73784954
复制相似问题