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.

Should I be able to use absolute path imports?

See original GitHub issue

Hi there,

I’m wondering if I’m supposed to be able to do something like

import ComponentWhatever from 'src/components/ComponentWhatever'

instead of one of these:

import ComponentWhatever from '@/components/ComponentWhatever'
import ComponentWhatever from '../components/ComponentWhatever'

I found the @ alias, but I’m not sure if absolute paths work out-of-the-box. I’m running into issues with my vue-cli-webpack project + Jest, and I think it’s because Jest is not translating the @ alias so I’d like to avoid it and just use the absolute path (if possible).

thanks!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
afontcucommented, Jul 19, 2017

Well, looks like I fixed my issue with Jest, just missed this regex pattern in my ModuleNameMapper:

"^@[/](.+)": "<rootDir>/src/$1",

I still don’t get why I can’t just use src/components/Component/ but since the @ alias is working…

1reaction
afontcucommented, Jul 20, 2017

@nickmessing finally! Now I understand, I was adding both the module.exports line and also src/ to every import statement. Thanks!

Then I added

"^components[/](.+)": "<rootDir>/src/components/$1",

to the ModuleNameMapper object from my Jest config and components/MyComponent worked fine both in dev and test environments.

Kudos to you! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why and How to Use Absolute Imports in React
Using absolute imports to better organize your React project is a great way. Relative imports are hard to follow and break during ...
Read more >
Use Absolute Paths with React - Better Programming
In this article, I will walk you through adding support for absolute paths to your project using Webpack.
Read more >
Absolute imports and module path aliases are a game changer!
So we can make use of paths to declare module aliases. The configuration would look something like this: The import after configuring module...
Read more >
Importing with Absolute Paths in JavaScript/TypeScript using ...
How to use absolute paths in JavaScript and TypeScript to improve your import statements, using the right webpack configuration.
Read more >
Absolute vs Relative Imports in Python
An absolute import specifies the resource to be imported using its full path from the project's root folder. Syntax and Practical Examples. Let's...
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