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.

docs: absolue imports

See original GitHub issue

Summary

Currently the docs recommend to set absolute imports within webpack. While this does work it only applies to webpack. If you are trying to eject from gatsby’s eslint rules or use other tools they will not know how to resolve these and will complain. For those using yarn 1 or 2 there is a better way:

the best practice is to set them in your package.json:

{
  "dependencies": {
      "hooks": "link:./src/hooks",
  }
}

I read this some where more authoritative but can’t find where (yarn docs I believe), I’ll keep looking and update this if I find the link.

https://yarnpkg.com/features/protocols

Motivation

This allows all tools to resolve these paths and preps you for yarn pnp.

Steps to resolve this issue

Draft the doc

Open a pull request

  • Open a pull request with your work including the words “closes #[this issue’s number]” in the pull request description

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
wardpeetcommented, Apr 7, 2020

Thanks @moonmeister, this shows how broken the ecosystem is, and Rome is being built.

I do feel your pain. I mostly use https://github.com/tleunen/eslint-import-resolver-babel-module which also tells me how to configure eslint, and because of babel, webpack has the right config as well. I haven’t thought of link: before so TIL 😃. The problem with link as stated above its yarn only and we still have to tell babel to transpile `node_modules/hooks, as stated in our docs https://github.com/gatsbyjs/gatsby/blob/master/docs/docs/add-custom-webpack-config.md#modifying-the-babel-loader

What if we keep things as is but add a section about “yarn” + “link” and using multiple tools like eslint/typescript/… We don’t want to advocate yarn too much but If it’s a small section, I think it’s okay.

1reaction
moonmeistercommented, Mar 12, 2020

Go for it! Thanks! Ping me when you open the PR and I’ll give it a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths. ... a common pattern is aliasing certain directories to use absolute...
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 >
Making Life Easier with Absolute Imports - React in Javascript ...
According to create-react-app Docs, We can use absolute imports in our react project by configuring a jsconfig.json / tsconfig.json (for ...
Read more >
MarcoGorelli/absolufy-imports: Automatically convert ... - GitHub
A tool and pre-commit hook to automatically convert relative imports to absolute.
Read more >
Absolute Imports | jest-preset-angular - GitHub Pages
TypeScript supports absolute imports. The preset (starting from v3.0.0) by default understands absolute imports referring to src, app, ...
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