question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue: svg-components with absolute imports not working

See original GitHub issue

Examples bug report

Example name

custom-server-typescript svg-components with-absolute-imports

Describe the bug

with the svg-components and absolute import the svg’s are not found

[ error ] ./components/cowsay.tsx
Error: Cannot find module 'svg/cat.svg' from '/Users/Projects/Playground/next/components' 

with tslint it does not see .jpg, .svg or any images as paths but instead as modules

Absolute Paths: Cannot find module 'svg/cat.svg.ts(2307) Cannot find module ‘images/pexels-photo.jpg’.ts(2307)

Relative Paths: Cannot find module ‘…/assets/svg/cat.svg’.ts(2307) Cannot find module ‘…/assets/images/pexels-photo.jpg’.ts(2307)

( as i side note i added paths to my tsconfig )

    "paths": {
      "images/*": ["assets/images/*"],
      "svg/*": ["assets/svg/*"]
    },

To Reproduce

https://github.com/PaulPCIO/next.js-issues-svg-absolute-imports-tslint

uncomment lines in components/ImageExamples.tsx

Expected behavior

  1. Expect SVG’s to import from absolute paths
  2. Expect tslint to recognise image importations

Screenshots

#issue 1:

import Absolute_CatSVG from 'svg/cat.svg'

image

#issue 2: RESOLVED image

System information

  • OS: macOS
  • Version of Next.js: 8.1.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
hoangvvocommented, Aug 24, 2020

Not sure if it is related but I recently stumbled upon this while using Next.js absolute import with babel-plugin-inline-react-svg and expected import SvgThing from '@/assets/svg/thing.svg' magically works. It won’t.

I guess because it is Babel related, not Webpack related so absolute import won’t work out of the box. Also this

My solution is to create a index.ts in assets/svg and reexports the svgs (export { default as SvgThing } from './thing.svg') and import from that file instead of directly from the .svg files.

7reactions
EzraBerendsencommented, Oct 26, 2020

Not sure if it is related but I recently stumbled upon this while using Next.js absolute import with babel-plugin-inline-react-svg and expected import SvgThing from '@/assets/svg/thing.svg' magically works. It won’t.

I guess because it is Babel related, not Webpack related so absolute import won’t work out of the box. Also this

My solution is to create a index.ts in assets/svg and reexports the svgs (export { default as SvgThing } from './thing.svg') and import from that file instead of directly from the .svg files.

If I use this I get the following error: index.ts: pass.get(...) is not a function

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS, Storybook, SVG and absolute import paths
There's an answer to a different problem that worked for me here. It might be because a conflict with Storybook's default SVG loader....
Read more >
Why and How to Use Absolute Imports in React
Relative imports has some more problems. Pretty hard to refactor. It becomes worse as you get further deeper into it. You need to...
Read more >
How to Import SVGs in a React and Vite app
Are you having difficulties importing SVGs into your React app? This is a problem that many developers face, especially when setting up a ......
Read more >
Importing a Component
You can configure your application to support importing modules using absolute paths. This can be done by configuring a jsconfig.json or ...
Read more >
Working with SVGs in React Native
The react-native-svg components use sentence case in naming to ... what users do on your web app, helping you troubleshoot issues faster.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found