无法将SVG导入到Typescript应用中,在react-scripts start
过程中抛出以下错误:
Failed to compile.
./src/components/Icons/Backward.svg (./node_modules/@svgr/webpack/lib?-svgo,+titleProp,+ref!./src/components/Icons/Backward.svg)
TypeError: Cannot read property 'children' of undefined
我是这样导入的:import {ReactComponent as SomeName} from './some/svg/file.svg'
你尝试恢复依赖了吗?
是的,我已经重新安装了所有的依赖。但仍然显示错误。
你在用户指南中搜索了哪些术语?
SVG, Typescript
环境
在 npx create-react-app info
上我得到这个:
Environment Info:
current version of create-react-app: 4.0.1
running from C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\create-react-app
(node:14276) UnhandledPromiseRejectionWarning: Error: Command failed: wmic os get Caption
"wmic" �� ���� ����७��� ��� ���譥�
��������, �ᯮ��塞�� �ணࠬ��� ��� ������ 䠩���.
at b (C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\envinfo\dist\envinfo.js:1:95039)
at Function.e.exports.sync (C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\envinfo\dist\envinfo.js:1:96902)
at e.exports (C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\envinfo\dist\envinfo.js:1:93256)
at e.exports (C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\envinfo\dist\envinfo.js:1:92403)
at C:\Users\Bunyamin\AppData\Local\Yarn\Data\global\node_modules\envinfo\dist\envinfo.js:1:91153
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Promise.all (index 0)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
重现问题的步骤
我的伙伴用typescript模板创建了一个全新的CRA,对我来说它可以工作。
我创建了一个新的项目,将所有文件移动到新项目中,但它仍然无法正常工作。
预期行为
SVG文件应该被导入为React组件。
实际行为
无法正常工作)
可复现的演示
很遗憾,我只能分享我的package.json文件:
{
"name": "cra-template",
"version": "0.1.0",
"private": true,
"license": "UNLICENSED",
"tags": [
"frontend",
"react",
"cra",
"typescript"
],
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"format": "prettier --write src/**/*.ts{,x}",
"lint": "tsc --noEmit && eslint src/**/*.ts{,x}"
},
"husky": {
"hooks": {
"pre-push": "yarn lint"
}
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"@hookform/error-message": "^2.0.0",
"@hookform/resolvers": "^2.8.1",
"@szhsin/react-menu": "^2.0.1",
"axios": "^0.21.1",
"classnames": "^2.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hook-form": "^7.12.2",
"react-icons": "^4.2.0",
"react-minimal-pie-chart": "^8.2.0",
"react-pro-sidebar": "^0.6.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"sass": "^1.38.1",
"typescript": "4.1.2",
"web-vitals": "^1.0.1",
"yup": "^0.32.9"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"eslint": "^7.32.0",
"eslint-config-airbnb-typescript": "6.3.2",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^6.0.0",
"prettier": "^2.3.2"
}
}
5条答案
按热度按时间gr8qqesn1#
我遇到了类似的问题。错误是
Attempted import error: 'ReactComponent' is not exported from '../../assets/images/tick.svg' (imported as 'TickIcon').
。真希望我知道问题出在哪里,因为它在默认设置下可以正常工作,但在我的项目中却不行。8ulbf1ek2#
我遇到了类似的问题。错误是
Attempted import error: 'ReactComponent' is not exported from '../../assets/images/tick.svg' (imported as 'TickIcon').
。真希望我知道问题出在哪里,因为它在默认设置下可以正常工作,但在我的项目中却不行。伙计,我的同事和我做了相同的设置,她的项目可以正常运行。她把代码推送到仓库,我拉取后,项目神奇地开始运行了......
ykejflvf3#
对于发现此问题的人,临时解决方法是:将所有SVG导入到index.js(注意js扩展名),然后从JS中导出。这样它就不会处理类型了。
7xllpg7q4#
这个问题已经被自动标记为过时,因为它没有任何最近的活动。如果没有发生任何进一步的活动,它将在5天后被关闭。
bq3bfh9z5#
有人能从维护者那里检查这个bug吗?