[Packager] doesn't resolve modules that are symlinks
As described in the browserify handbook you can use symlinks in the node_modules folder to reference modules. This is mainly useful for preventing ../../../../ requires.
With a symlink setup from the project root setup like:
ln -s src/app node_modules/app
in any file in the project you can:
var app = require('app');
var somethingElse = require('app/lib/something-else');
However, the packager can't resolve them: Requiring unkonwn module "app".
Edit: Check out this repo to easily reproduce the error above:
https://github.com/JaapRood/react-native-packager-symlink-bug
is it a bug or is it by design? symlinks would be really handy actually :(
Just ran into this trying to develop a npm library locally using npm link
I thought we supported this use case. Thanks
Perhaps related, relative requiring a module above the iOS/React Native directory fails the same way:
// in index.ios.js
var Auth = require('../common/auth');
Error: Requiring unknown module "../common/auth". If you are sure the module is there, try restarting the packager.
I tried symlinking that higher-level directory within the React Native project to fix, but it failed as well, leading me to this issue.
I finally had some time to make an example to prove it's not working. Check out this repo which references the example app component through a symlink, which doesn't get resolved.
https://github.com/JaapRood/react-native-packager-symlink-bug
Now, if only I had any idea on why.
I have an internal fix for this. Aiming to get it out next sync :)
Awesome @amasad - looking forward to this, will make lib development easier
Great @amasad, that would be immensely helpful!
@amasad no pressure, but when to expect next sync? :) will it be in 0.6.0 release?
still an issues as to 0.6.0 and 0.7.0-rc ...
That's strange. I'll give @JaapRood's test project a spin see what the problem is.
:+1: Same problem here. Symlinks are very useful, especially when I don't want to make a node package just to share a component between two related React apps.
it generally usefull for many global acessible parts of your application: constants, configs, reusable components replacing react-native base components (styled Text for example) and none of other methods of requiring modules from applications root seen to work too :( this is why i'm hoping for this one so much.
@amasad - any updates here?
:+1:
Figured out a little more about the cause -- the issue goes away when switching to the Node-based watcher instead of the Watchman-based one. The watchman homepage says, "Watchman does not follow symlinks. It knows they exist, but they show up the same as any other file in its reporting." so this could be the root case.
Support for symlinks seemed to get pretty strong pushback in https://github.com/facebook/watchman/issues/105 so perhaps the packager could use a combination of Watchman for most files and the Node watcher for symlinked packages. npm linked packages reside in the first node_modules directory (you'd have to go out of your way to end up with node_modules/pkg1/node_modules/<linkedpkg>), so that might simplify things a little. I tried adding a --noWatchman option to the packager but ran into fd limit issues unfortunately.
The npm link workflow is really important for anyone developing a React Native module so I do think this is a priority for the external RN community.
I'm not sure if this is related or not. I have a symlinked folder for some of my js files in the project root. These are some internal libraries that are part of another repo. I am importing them using require('./lib/file') where lib is a symlink to a folder. This is working in the packager in 0.7.1 and not working in 0.8.0-rc. I am using io.js.
I figured I'd put this here in case some code is changing in 0.8.0-rc related to this bug...
@amasad - seems like this was working for some people in 0.7.1 and not in 0.8.0-rc, as reported in #2076
For 0.8.0-rc, the problem is Fastfs._getRoot returns null, when the local module located outside of the app dir, because the packager can only resolve modules within the app dir by default.
To solve the problem, I must launch the packager with an additional argument --root=<path_to_local_module>.
I'm not sure what's the best solution for this issue?
I use 0.8.0, after close all terminal, xcode, restart. then everything is OK.