热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CannotloadSVGinReactapplication

[x]Iamrunningthelatestversion[x]Icheckedthedocumentation


  • [x] I am running the latest version

  • [x] I checked the documentation and found no answer

  • [x] I checked to make sure that this issue has not already been filed

  • [x] I'm reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

Utilize an SVG file using the

1

tag in a React application.

Current Behavior

The application fails to compile with

1
nx serve

.

Failure Information (for bugs)

1
2
Module parse failed: Unexpected token (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.


Steps to Reproduce

I personally discovered this when trying to use the Ionic React components in an Nx workspace, so will provide these steps to reproduce.

  1. Generate a new Nx workspace with a default React application


  2. 1
    yarn install /react


  3. Wrap the contents of the default
    1
    app.tsx

    JSX with

    1



  4. 1
    nx serve


If you can provide steps to reproduce from scratch, that would be enormously appreciated (i.e. where the first step is

1
npx create-nx-workspace repro-workspace

)

Context

Please provide any relevant information about your setup:

  • Nx 8.6.0

  • /react 4.10.0-rc.3

Failure Logs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ERROR in /Users/devinshoemaker/code/ionic/test-nx-ionic/node_modules/ionicons/dist/ionicons/svg/md-quote.svg 1:0

Module parse failed: Unexpected token (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

>



ERROR in /Users/devinshoemaker/code/ionic/test-nx-ionic/node_modules/ionicons/dist/ionicons/svg/ios-school.svg 1:0

Module parse failed: Unexpected token (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

>



ERROR in /Users/devinshoemaker/code/ionic/test-nx-ionic/node_modules/ionicons/dist/ionicons/svg/md-arrow-round-down.svg 1:0

Module parse failed: Unexpected token (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

>


Other

I have found one other person that reported a similar issue, though it's not Ionic specific. The solution proposed here involved overriding the default webpack config that Nx uses for it's React applications, however that config did not work for me, and I am not adept enough with webpack yet to get this working. I have been inspecting the

1
create-creact-app

1
webpack.config.js

(which is what Ionic uses in it's generated projects) and will continue to try and get an override working, but this does seem like something that should be available out of the box.

1
create-react-app

applications are quite popular, so I imagine many people will eventually run into this issue when using React with Nx.

https://stackoverflow.com/questions/56848952/cannot-load-svg-in-nrwl-react-workspace

该提问来源于开源项目:nrwl/nx

Talking about the linting error in




1
webpack.config.js

, I faced the same error but I ignored it and used

1
require

because




1
'/react/plugins/webpack'

was not an ES module.


   



推荐阅读
author-avatar
mobiledu2502916457
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有