typescript - Typescript 无法解析声明为 webpack 别名的模块
问题描述
我已经设置了我的 webpack 配置来解析development
或的配置文件production
。
文件夹结构为:
Project
|
+-- config
|
+-- development.ts
+-- production.ts
配置文件只是一个默认导出的对象:
const config = {
errorUrl: 'http://localhost:3000/error',
successUrl: 'http://localhost:3000',
branch: 'Branch',
tag: 'Tag1',
username: 'Demo client DEV',
debug: true,
};
export default config;
我paths
在我tsconfig.json
的 with中设置baseUrl
为项目根.
:
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"paths": {
"@/pages/*": ["src/pages/*"],
"@/components/*": ["src/components/*"],
"Config": ["config/*"]
},
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"checkJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": false,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": [
"node_modules",
"dist",
"coverage",
"functional-test",
"webpack.config.js",
"jest.config.js",
"**/*.spec.ts",
"**/*.spec.tsx"
],
"typeAcquisition": {
"include": ["jest"]
}
}
我在中添加了决心webpack.config
:
resolve: {
extensions: ['.js', '.ts', '.tsx', '.jsx'],
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })],
alias: {
Config: path.resolve(__dirname, 'config/', argv.mode)
}
},
development.ts
当我在这样的组件中导入文件时,我认为应该解决这个问题import config from 'Config';
但是我收到以下 TS 错误:
TS2307: Cannot find module 'Config' or its corresponding type declarations.
我没有看到我在这里做错了什么?
当我console.log(path.resolve(__dirname, 'config/'));
似乎也是正确的路径时,不确定为什么它没有解决。
解决方案
推荐阅读
- python - pyttsx3 ModuleNotFoundError:没有名为“pythoncom”的模块
- python - 如何删除 PixelArray 的元素
- django - 在 django 中使用“ImageKit”对已关闭文件进行 I/O 操作
- arrays - 在 Shell 脚本中将 SED Commnd 的字符串输出存储在数组中
- python - 无法通过 loaddata 将 JSON Postgresql 数据库备份上传到新的 Django Heroku 应用程序
- c# - 有人可以解释为什么会发生此错误
- .net - ML.NET:使用 GUID 作为 ID 的推荐
- kubernetes - 如何识别谁在 Kubernetes 中编辑了 yaml 或属性文件?
- apache-kafka - Spring cloud stream kafka binder 连接到 docker-compose kafka
- .net-core - 使用 Apple 登录 - 未完成注册。点网核心