react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Packager] doesn't resolve modules that are symlinks

Open JaapRood opened this issue 11 years ago • 114 comments

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

JaapRood avatar Apr 03 '15 06:04 JaapRood

is it a bug or is it by design? symlinks would be really handy actually :(

romanonthego avatar May 13 '15 09:05 romanonthego

Just ran into this trying to develop a npm library locally using npm link

cgarvis avatar May 16 '15 03:05 cgarvis

I thought we supported this use case. Thanks

amasad avatar Jun 02 '15 21:06 amasad

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.

mars avatar Jun 06 '15 18:06 mars

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.

JaapRood avatar Jun 08 '15 06:06 JaapRood

I have an internal fix for this. Aiming to get it out next sync :)

amasad avatar Jun 15 '15 21:06 amasad

Awesome @amasad - looking forward to this, will make lib development easier

brentvatne avatar Jun 15 '15 23:06 brentvatne

Great @amasad, that would be immensely helpful!

JaapRood avatar Jun 15 '15 23:06 JaapRood

@amasad no pressure, but when to expect next sync? :) will it be in 0.6.0 release?

romanonthego avatar Jun 25 '15 12:06 romanonthego

still an issues as to 0.6.0 and 0.7.0-rc ...

romanonthego avatar Jun 26 '15 06:06 romanonthego

That's strange. I'll give @JaapRood's test project a spin see what the problem is.

amasad avatar Jun 30 '15 02:06 amasad

:+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.

idmontie avatar Jul 02 '15 18:07 idmontie

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.

romanonthego avatar Jul 03 '15 09:07 romanonthego

@amasad - any updates here?

brentvatne avatar Jul 08 '15 19:07 brentvatne

:+1:

romanonthego avatar Jul 09 '15 08:07 romanonthego

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.

ide avatar Jul 11 '15 00:07 ide

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...

goatfish avatar Jul 14 '15 18:07 goatfish

@amasad - seems like this was working for some people in 0.7.1 and not in 0.8.0-rc, as reported in #2076

brentvatne avatar Jul 21 '15 16:07 brentvatne

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?

xinthink avatar Jul 23 '15 14:07 xinthink

I use 0.8.0, after close all terminal, xcode, restart. then everything is OK.

zhaiduo avatar Jul 29 '15 05:07 zhaiduo