Uh oh!
There was an error while loading. Please reload this page.
Made webpack respect NODE_PATH environment variable - #476
Conversation
ghost
commented
Aug 22, 2016
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
| }, { | ||
| 'process.env.NODE_ENV': NODE_ENV | ||
| 'process.env.NODE_ENV': NODE_ENV, | ||
| 'process.env.NODE_PATH': NODE_PATH |
There was a problem hiding this comment.
env.js is only used for variables injected into the app.
Doesn't seem like it's useful to expose it to the path.
env.js is only for variables injected into the app.
ghost
commented
Aug 22, 2016
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
gaearon
commented
Aug 23, 2016
Definitely, we want to have feature parity with how Browserify treats |
jimmyhmiller
commented
Aug 23, 2016
I couldn't find exactly where browserify handles NODE_PATH, but this test seems to suggest they support NODE_PATH the way node does. I've added support for multiple paths. I moved the logic to paths.js. I'm not sure if this is the right place, but it seemed to make the most sense so that I don't duplicate the logic in the prod and dev config. I have not tested this on windows myself, but I did test on my mac that local paths resolve when build and testing. |
jimmyhmiller
commented
Aug 29, 2016
Glad to see this added to a milestone! Any changes that need to before made before it could be merged? |
gaearon
commented
Sep 2, 2016
Can you please verify that newly added testing with Jest also works? |
gaearon
commented
Sep 2, 2016
Out in 0.4.0. Thanks again! |
amandapouget
commented
Sep 19, 2016
It is really confusing here how to use this feature. Could someone give an example of exactly what you need to enter into your command line to make the sample App run with this line: Tried a lot of variations of things like: |
gaearon
commented
Sep 19, 2016
@mandysimon88 If you use Bash on OS X or Linux, this should work: NODE_PATH=./src npm start
NODE_PATH=./src npm run build
NODE_PATH=./src npm testIf you use Cmd on Windows: NODE_PATH=./src&&npm start
NODE_PATH=./src&&npm run build
NODE_PATH=./src&&npm testNote that lack of whitespace on Windows is intentional. Does this help? |
gaearon
commented
Sep 19, 2016
(I understand it’s frustrating this feature isn’t documented. It was added as a stopgap measure so we’d prefer not to advertise it widely. Ideally we’ll figure out some different solution to this before 1.0.) |
amandapouget
commented
Sep 21, 2016
Yes, this was really helpful. We ended up trying about 20 different variants on NODE_PATH= ? before stumbling on the answer. I can understand your desire not to advertise widely, but seriously, relative paths are a pain… I’m bringing over 33k lines of code from a previous project based on angular, and faced with updating the paths of every single import in every file. You can imagine the task.
|
gaearon
commented
Sep 21, 2016
Oh I can imagine. :P |
There's been numerous requests for Create-React-App to support having imports resolved relative to the "src" folder. The semi-documented solution is to have a NODE_PATH environment variable, which will be used in the resolution process. It's apparently also possible to specify that variable in a file named ".env". References: react/create-react-app#476react/create-react-app#693react/create-react-app#741
There's been numerous requests for Create-React-App to support having imports resolved relative to the "src" folder. The semi-documented solution is to have a NODE_PATH environment variable, which will be used in the resolution process. It's apparently also possible to specify that variable in a file named ".env". References: react/create-react-app#476react/create-react-app#693react/create-react-app#741
mileung
commented
Mar 24, 2017
When I run
Am I missing something? |
jimmyhmiller
commented
Mar 24, 2017
In order to make that import you would need to have I personally recommend putting this in your package.json. |
This addresses #253. Nothing should change by default, but you are able to set your NODE_PATH environment variable if you want absolute path imports.
Test Plan
I tested this by changing
import App from './App';toimport App from 'App';in index.js. Without settingNODE_PATHrun and build will now fail because it can't find the module. After setting the theNODE_PATHto `./template/src' the project both builds and runs.I also generated a new project, which you can find here that uses this absolute import. I did the equivalent change in the tests directory and the tests pass.
Let me know if you have any questions or any suggestions.