Skip to content
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

Resolve modules absolutely #342

Closed
rovansteen opened this issue Dec 4, 2016 · 9 comments · May be fixed by RiftNemesis/next.js#1
Closed

Resolve modules absolutely #342

rovansteen opened this issue Dec 4, 2016 · 9 comments · May be fixed by RiftNemesis/next.js#1

Comments

@rovansteen
Copy link
Contributor

Is there any way right now to resolve modules absolutely relative to the project directory?
For example inside a file in the pages directory:

import Title from 'components/title'

where the components directory is next to the pages directory.
This would be nice so that we don't have to write long relative paths for nested directories.

@sergiodxa
Copy link
Contributor

You can create a components directory inside the node_modules directory and then use components/title. But since the babel-loader in the webpack configuration exclude node_modules you can't made use of JSX and everything Node.js don't support right now.

When #222 be merged you will be able to able to configure aliases (or custom modules directories) in the webpack configuration.

Maybe this can be added to the next.js configuration, but then everyone should use a components directory next to the pages directory to get this aliases to work. I'm not sure this is something the next.js team want to force in every developer who use it.

@arunoda
Copy link
Contributor

arunoda commented Dec 5, 2016

@rovansteen @sergiodxa Next.js supports NODE_PATH env variable.
See: #233

You can . as the NODE_PATH and do absolute imports. You can add multiple of them as well separated by the OS's seperators. ( ":" in Unix and ";" in windows )

@rovansteen
Copy link
Contributor Author

@arunoda yeah I saw that issue but that's not working for me because the files are imported that way are not transpiled through babel. Is that correct?

@sergiodxa that was something I tried as well but ran into the issue you described. Guess I'll have to wait until #222 is merged. Thanks anyway!

@arunoda
Copy link
Contributor

arunoda commented Dec 5, 2016

@rovansteen no. It should work properly.
Next.js wepack instance is configured to read NODE_PATH.

If that's not working it's a bug.

@foxhound87
Copy link

I suggest babel-root-import

It can be configured to support multiple paths.
I found this approach very useful when doing refactoring.

@rovansteen
Copy link
Contributor Author

@arunoda when using NODE_PATH I'm getting the classic SyntaxError: Unexpected token import error so it doesn't seem to be transpiled through babel. Same goes for using babel-root-import @foxhound87.

@rovansteen
Copy link
Contributor Author

Correction, babel-root-import does work, got my babel configuration wrong. Thanks for the tip @foxhound87. But the issue still remains for the NODE_PATH.

@arunoda
Copy link
Contributor

arunoda commented Dec 8, 2016

@rovansteen Yep. NODE_PATH is not working properly.

@arunoda
Copy link
Contributor

arunoda commented Jan 1, 2017

I've look at this again.
We do support NODE_PATH but it's not for this case. We support it to load NPM modules.
(Which are pre-transpiled)

We do support absolute modules via NODE_PATH in webpack environment. But for SSR, it won't work well. I tried fixing a lot of stuff, even hijacking node's module system or introduce a different env variable. This is doable, but feels hacky.

Since we've a solution via babel-root-import, it's not worthy to go for a hack.

@arunoda arunoda closed this as completed Jan 1, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
hoodsy referenced this issue in homeroom-live/homeroom Jul 1, 2018
+ I have replaced redirects with copies of main dashboard page. This way user should be less confused about what's happening, since navigation indicator should follow his clicks.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants