【发布时间】:2020-11-03 04:25:38
【问题描述】:
我试图在我的 react 项目上启用绝对导入,该项目仅使用 typescript 编译。 有人告诉我应该添加 baseUrl 配置选项,它告诉编译器在哪里查找。但是当我编译我的代码时,我得到了以下错误:
//Broken
import { useMeQuery } from 'generated/graphql';
//Works
import { useMeQuery } from '../../generated/graphql';
Failed to compile.
./src/components/Pages/HomePage.tsx
Module not found: Your application tried to access generated, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.
如果我将路径添加到我的 tsconfig,则会收到以下错误。
/Users/pkwadsy/src/projects/belemon/web/.yarn/$$virtual/react-scripts-virtual-f1dd360a52/0/cache/react-scripts-npm-4.0.0-675c13e360-508ceb13ad.zip/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
appTsConfig.compilerOptions[option] = value;
^
TypeError: Cannot assign to read only property 'paths' of object '#<Object>'
at verifyTypeScriptSetup (/Users/pkwadsy/src/projects/belemon/web/.yarn/$$virtual/react-scripts-virtual-f1dd360a52/0/cache/react-scripts-npm-4.0.0-675c13e360-508ceb13ad.zip/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)
at Object.<anonymous> (/Users/pkwadsy/src/projects/belemon/web/.yarn/$$virtual/react-scripts-virtual-f1dd360a52/0/cache/react-scripts-npm-4.0.0-675c13e360-508ceb13ad.zip/node_modules/react-scripts/scripts/start.js:31:1)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.external_module_.Module._load (/Users/pkwadsy/src/projects/belemon/web/.pnp.js:26999:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
这对我没有任何意义,任何帮助将不胜感激:)
这是我的配置文件。
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"src": ["./src"]
},
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"plugins": [
{
"name": "typescript-plugin-css-modules",
"options": {
"classnameTransform": "camelCase"
}
}
]
},
"include": ["src"]
}
【问题讨论】:
-
任何更新/解决方案? ????
标签: reactjs typescript path