-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document src/node_modules as official solution for absolute imports #1065
Comments
I want to help but I will need someone to shadow. |
Feel free to jump on either of those issues and let me know if you need any help! |
This is a very interesting idea! I'm wondering though if it'd make more sense to support a top-level packages directory (exactly like lerna) and symlink that to
The reasoning for this is that typically your external modules are general purpose and not application-specific (therefore not desirable in the application source directory), and to support something like lerna. Maybe you want to do code splitting via npm packages? This would allow you to support lerna and create-react-app in the same project. For selfish reasons, this would allow me to use create-react-app to kitchen sink/playground a bunch of lerna components. |
what's the issue with |
Most people don't know about Ultimately I just want both ways to work, and we can document them. People can then choose whichever way they prefer. |
I believe this is what I suggested in the opening comment (but only as opt-in):
It has to be opt-in though, please read the discussion about Windows issues in #741. |
I personally like the idea of a But right now there's some problems with linters:
|
Another way of managing absolute paths that might have come up but could not find it in We've been using wix's wml
As project's readme states — they just use Sure it's not an ideal solution but it seems to work really nice and does not force you to follow any conventions so it's easily can be applied to any project. What do you think? |
I've created a link However, when I run my tests, jest gives me the following error:
on the line where I am doing that import. Is there something I need to add to my jest config to make this work properly? |
Please see the first post in this thread 😉 |
will there also be a way to build and npm publish these as separate components? |
@azz That's not really the reason why I ask this question. I mean adding what would be the component equivalent of |
@viankakrisna Indeed, sort of the |
If I understand correctly, the only reason we are not using Further, we could enforce the direct folders or files inside import React from 'React' won't conflict with the |
@doshisid Wouldn't that only work in case sensitive filesystems? If so, 👎 |
We could find a common case for all filesystems and enforce that. Like maybe prefixing with _ |
For the absolute import, beside of In the fractal project structure
for code in And with =============== UDATE: =============== |
For me using node_modules inside src is not beeing a option because jest is crashing. Very annoying. So my solution was using NODE_PATH=src in my package.json and in WebStorm/Intellij I simply did: Right click on src folder -> Mark Directory as -> Sources Root Voi lá! Everything is working fine! (go to files, open classes, moving files to another directory automatically change the references) A note about NODE_PATH is that in the current docs (Node 8.1.2) is not saying that NODE_PATH is legacy or will be deprecated but that it is not used as it was used before:
|
the docs quote above convinces me that it's not deprecated, contrary to what OP has implied. |
NODE_PATH require an extra dependency to be cross-platform, tools has to specify the extra variable, and it works here only because it's the toplevel (as the react app isn't a module that can be imported in another module), otherwise it would introduce extra complexity there too. But pick whatever you prefer: I'm not fond of /src/node_modules either, just is the most pragmatic in the end given the way Node is at the moment. |
I tried playing around with using this approach in my project and I noticed |
For absolute imports, it seems like it'd be a lot better to have some kind of special prefix and set up webpack's alias functionality for it. For example, my personal projects use: {
resolve: {
alias: {
"@": path.resolve(__dirname, 'src')
}
}
} ...after which you can reference files via... import App from '@/App' // the file is at `src/App/index.jsx` You can also inject the same schema into Jest: "moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
} It keeps a clear distinction from npm-tracked |
Solution using .env to set NODE_PATH (commit c148630) appears unreliable. Discussions about absolute imports for create-react-app are ongoing; making a symlink seems the currently recommended approach. I call it 'src' for clarity. See e.g. facebook/create-react-app#1065
Closing in favor of #1333. There's been a few underlying issues with Jest which have been fixed so we can take another shot at this. |
Sorry to resurrect an old issue, though with #1333 being not quite ready, is |
After a long discussion in #741 it seems to me that
src/node_modules
seems like the best solution for absolute imports.It has the following benefits:
ln -s src/node_modules src/packages
and use the "nicer"packages
(or any other) folder.This issue is a call to help to make this approach the official one. We need to:
If you want to work on either please leave a comment and then submit a PR!
Cheers.
The text was updated successfully, but these errors were encountered: