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.

Using absolute instead of relative paths

See original GitHub issue

I would like to use absolute paths instead of relative paths in Gatsby if it’s possible.

For example, in the tutorial for using component css

import React from "react";

import Container from "../components/container";

export default () => (
  <Container>
    <h1>About CSS Modules</h1>
    <p>CSS Modules are cool</p>
  </Container>
);

instead of using ../components... I would like to use src/components/...

I’ve done some google’ing, but haven’t been able to figure it out. I’ve used .env file in the past with create-react-app. Is there a way to edit the .babelrc file in Gatsby or some other way to accomplish this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
dustinhortoncommented, Dec 28, 2017

Check out gatsby-plugin-resolve-src by @alampros. Would that work? Wouldn’t be src/components/... but rather components/..., which I find preferable.

I’d love something similar for Sass. I haven’t spent too much time digging for something better, but right now I do the following in gatsby-node.js:

const path = require('path')

exports.modifyWebpackConfig = ({ config, _stage }) => {
  return config.merge({
    resolve: {
      alias: {
        styles: path.resolve(config._config.context, 'src/styles'),
      },
    },
  })
}

…which allows for imports like this in Sass:

@import '~styles/variables`

…as opposed to what’s required currently:

@import 'src/styles/variables`
2reactions
dustinhortoncommented, Mar 26, 2018

@alampros Thanks for the awesome work on gatsby-plugin-resolve-src.

I tried your proposed solution, but couldn’t get it to work, at least in my project. FWIW, relative imports don’t need that variable usage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Absolute vs Relative Path - Which Should You Be Using?
"Using absolute links for images allows you to assert ownership in the image search results, which reduces the likelihood of duplicate content ...
Read more >
Absolute and Relative Pathnames in UNIX - GeeksforGeeks
A path is a unique location to a file or a folder in a file system of an OS.A path to a file...
Read more >
Difference between Relative path and absolute path in javascript
The difference between relative and absolute paths is that when using relative paths you take as reference the current working directory ...
Read more >
What is the difference between a relative and absolute path?
In the example above, the absolute path contains the full path to the cgi-bin directory on that computer. The relative path begins with...
Read more >
What are the differences between absolute and relative paths?
Absolute paths are useful, but they're not always efficient. If you know where the widgets directory, which contains button , is located. then ......
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