facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.42k stars 24.37k forks source link

[Workarounds] Packager unable to resolve module from /Users/node_modules/ #4968

Closed satya164 closed 5 years ago

satya164 commented 8 years ago

There are various issues scattered around the repo related to this issue. Basically what happens is, for some packages, when you try to require some-module in a file, for example,

var someModule = require('some-module');

It is unable to resolve the package and the following error appears,

Unable to resolve module some-module from /Users/username/projectname/AwesomeProject/index.js: Invalid directory /Users/node_modules/some-module

This error message is a symptom of the packager not being able to find some-module. It'll walk up the directory tree until it finds node_modules/some-module. It just so happens that /Users is the last directory to try, hence the weird /Users/node_modules directory in the error message (h/t @philikon).

Workarounds

Currently, the workarounds seem to be,

brentvatne commented 8 years ago

cc @amasad

amasad commented 8 years ago

cc @martinbigio @davidaurelio @cpojer

amasad commented 8 years ago

That's really strange. Next time it happens can you list your watch roots? watchman watch-list

hufeng commented 8 years ago

yes, we met the same problem.

martinbigio commented 8 years ago

Do we have a consistent repro case? I've experienced this too.

Hybrid-Force commented 8 years ago

Same problem here on version 0.17.0.

$ watchman watch-list
{
    "version": "3.8.0",
    "roots": []
}
arypurnomoz commented 8 years ago

I also got this problem when requesting for platform=ios

udnisap commented 8 years ago

Do we have a way to recreate this? Any steps that might recreate.

sgonyea commented 8 years ago

I suspect that this is actually an npm bug. It seems that npm is secretly deleting installed packages in my project's node_modules directory, when I install an unrelated package. When it blows up with an error, it references /Users/node_modules/ because (I'm guessing) it's just searching up the directory tree 3-4 levels before giving up.

re-running npm install <module> —save for each of the missing modules fixes this issue for me.

edit: My guess is that React Native's complex dependency structure is just exposing npm's bugs.

edit 2: Yeah, you pretty much want to blow away your project's node_modules directory and do a fresh npm install. I think the issue is extra common if you depend on modules that React Native also depends on (say, lodash). npm just gets into a weird state with all the, ahem, flux :).

udnisap commented 8 years ago

Is this only seen in npm 3?

npm 3 uses flat dependency structure. essentially node_modules are not resides in individual packages but only on the top level. if we have references like {somepackage}/node_modules/... we will have issue. Does it ring any bells?

sgonyea commented 8 years ago

I'm using npm 3.5.2. I'm not sure if it applies to npm 2.

amasad commented 8 years ago

I don't think we've tested on npm 3. Although the node resolution algorithm (which we implement) should still work. Are all of you seeing this using npm 3?

Hybrid-Force commented 8 years ago

@amasad No, I am using npm v2.14.7

pcottle commented 8 years ago

:+1: I'm seeing this too with npm 2.14.15 and node v5.1.0. I eventually got a fix by following @sgonyea's advice -- blew away the react-native npm module and reinstalled.

Interestingly none of:

fixed it for me. I experienced this after switching between two different React Native projects (one that relies on an older version). I ran the packager for both so maybe that will be part of the repro? It's not clear.

Either way I'm unblocked but this seems like a pretty nasty but. And googling for it yields a bunch of unrelated issues :P

skevy commented 8 years ago

@pcottle and whoever else is/has experienced this issue - putting up a repo (with node_modules checked-in) would be AWESOME to help debug this. I too have experienced it, and so have people on my team (on both npm 2 and npm 3). In our case, resetting cache and doing watchman watch-del-all has fixed it (though obviously that's not the ideal solution)...but I realllllly wanna see this fixed, and would be happy to dive in to do the fixing. So if anyone can repro and put up the test case on Github, that would be super helpful. Obv post node version along with it so that we can duplicate the env.

pcottle commented 8 years ago

@skevy will do if I run into it again! My bad, I hadn't thought of checking in the repos -- usually i think of the package.json as being the source of truth (so all else doesnt matter).

Others seem to be reporting that restarting helps, so I'm worried its due to something in-memory (which means repro-ing is harder).

Anyways I'm getting a new laptop from FB in a few days which means a full reinstall of everything, so I'll post back if I run into it again

philikon commented 8 years ago

FWIW, this error message is just the symptom of the packager not being able to find some-module. It'll walk up the directory tree until it finds node_modules/some-module. It just so happens that /Users is the last directory to try, hence the weird /Users/node_modules directory in the error message.

jukkatupamaki commented 8 years ago

npm cache clean && npm install fixed my project.

I encountered this bug after upgrading React Native from 0.14 to 0.17. After the upgrade I installed a new module (react-native-swipeout) and tried to require it in my code but the module could not be located. At this point I also tried to require a new JS file I had created. That did not work either.

I'm using nvm 0.29.0, Node.js 5.3.0, and npm 3.3.12.

Edit: After the first time the problem disappeared I added another module (rn-router) and the same happened again. I got it working again when I closed the emulator and the packager script, and then ran clean & build in Xcode.

martinbigio commented 8 years ago

Until we find the root cause for this it would be great showing a better error message with possible ways to fix the problem. For instance:

Someone wants to send a PR for this? ;)

udnisap commented 8 years ago

Looks like this code relays on the dependency graph of the package structure. https://github.com/facebook/react-native/blob/master/packager/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js#L211-L231

btsai commented 8 years ago

I banged my head against a wall for the last 15 minutes before finding this thread. I had added a new npm library (react-native-i18n) and when I did a new build, it was throwing the same error message "Unable to resolve module react-native-i18n from /Users/node_modules". I resolved this by killing the react-native/packager script terminal window. Thanks for having this thread!

react-native 0.17.0 npm -v 2.14.15 node -v v5.3.0

alvaromb commented 8 years ago

I keep getting this error every time I install a dependency from Github. There are some deps that do not support RN 0.16.0 and onwards and I have cloned them in order to ignore .babelrc. I cant make those work.

frederickfogerty commented 8 years ago

@alvaromb find node_modules/ -type f -name .babelrc | grep -v packager | xargs rm # Removes all .babelrcs from node_modules is your friend

alvaromb commented 8 years ago

Thanks @frederickfogerty !

afilp commented 8 years ago

I have the same problem with 'react-native-maps', whatever I try the build fails with this message that the module cannot be found.

tomkur commented 8 years ago

Similarly to @btsai, restarting packager fixed the problem for me. Is adding modules while packager is running supported?

I think the module resolution is working properly, i.e. first node_modules from project directory is checked and then after unsuccessful match (probably due to packager not noticing the change), the resolver goes up the file system hierarchy, up to /Users/node_modules/<module> and fails with the last error message, which will be Unable to resolve module <module> from /Users/node_modules/<module>

I've looked at react-native/packager/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js which I believe is the culprit here, but it would be good if someone more knowledgeable looked at it as well.

29er commented 8 years ago

wow this was a really annoying bug, spent a couple of hours on this one. so far this has worked: Delete/install the node_modules folder and reset cache

rm -rf node_modules && npm install rm -fr $TMPDIR/react-* or node_modules/react-native/packager/packager.sh --reset-cache

nowaits commented 8 years ago

+1 platform=ios

MatthewRDodds commented 8 years ago

@btsai's tip did it for me, was still encountering the issue after running both:

npm install --force
npm cache clean && npm install

But after closing the react-native/packager script terminal window and the simulator and building from scratch all was well.

aylarov commented 8 years ago

It's kind of big issue, met it first time after upgrading to 0.18.1 and actually workarounds don't work for me. It happens with the native module we develop:

Unable to resolve module createReactNativeComponentClass from /Users/aylarov/Work/iOSReactApp/VoximplantDemo/node_modules/react-native-voximplant/VoxImplant.ios.js: Invalid directory /Users/node_modules/createReactNativeComponentClass
Sexycrets commented 8 years ago

Same here. After upgrading to 0.18.1: UnableToResolveError: Unable to resolve module module from /Users/MyUser/MyAppName/node_modules/app-module-path/lib/index.js: Invalid directory /Users/node_modules/module This error is consistent across at least two modules that I tried to install. All modules installed previous to the react-native upgrade work well.

Update: I cloned my project again in a different folder to see if there was something else wrong on my end and now even the modules that were working before give the same error:

UnableToResolveError: Unable to resolve module react-native-picker-android from /Users/MyUser/untitled folder/node_modules/react-native-picker/index.js: Invalid directory /Users/node_modules/react-native-picker-android
    at ResolutionRequest.js:356:1

Is there any quick workaround to at least be able to keep working with the current modules before this issue get fixed? Unfortunately now I can't even launch the app.

Thank you!

joewood commented 8 years ago

Seeing this too, on and off when adding react-native-facebook-login. Error is:

uncaught error Error: UnableToResolveError: Unable to resolve module invariant from /Users/joewood/code/my-app/node_modules/react-native/Libraries/ReactIOS/NativeMethodsMixin.js: Invalid directory /Users/node_modules/invariant

  • react-native@0.18.1
  • node@4.2.3
  • npm@2.14.16

I see this is from the bundle script and/or the packager. Seems intermittent.

Sexycrets commented 8 years ago

I tried to downgrade to 0.17 using npm install --save react-native@0.17 but there is still no way I can get the project to work again. What else can be done to go back to a working copy ?

thomasm0 commented 8 years ago

I had this a few times and unfortunately @29er's fix didn't seem to work for me. I am referring to this fix: watchman watch-del-all npm cache clean && npm install

What did seem to fix it (after killing the emulator and npm packager script) was deleting the node_modules folder and running the basic app init commands:

  1. react-native init AwesomeProject
  2. npm install

Running react-native init again won't destroy your project :) It will prompt you if you want to override the react-native generated files.

aylarov commented 8 years ago

What did seem to fix it (after killing the emulator and npm packager script) was deleting the node_modules folder and running the basic app init commands:

  1. react-native init AwesomeProject
  2. npm install

Didn't work for me

thomasm0 commented 8 years ago

Maybe also try running npm cache clean or @29er's complete suggestion after deleting the node_modules directory?

@aylarov I wonder if it's the way that you're exporting your node_module perhaps? I had some issues with a specific module but after various attempts with trial and error to originally fix this I can't be sure.

aylarov commented 8 years ago

My issue was with my module, I fixed it, but there is definitely some issue if you copy/paste contents of the module w/o using npm install , I guess it is related to npm cache somehow.

Sexycrets commented 8 years ago

Thank you @thomasbandit your solution fixed it:

  1. react-native init AwesomeProject (1a. copy my package.json to the folder) (1b. say "no" to all override requests)
  2. npm install

At least now I can continue development with no new modules. If I try to install a new module with npm and require it in the code: var ModulePath = require('app-module-path'); I get the same error ```Invalid directory /User/node_modules/module This looks a lot like a fairly simple path error. Is there any way to override or expand the list of path where it is looking for the node_modules?

thomasm0 commented 8 years ago

@Sexycrets I think that if you just add any new modules to your package.json before running that npm install you should be good.

MattFoley commented 8 years ago

I'm actually having this same problem with my Docker build jobs. This is just when executing react-native bundle, not when the packager is running in a terminal. There's no watchman on this container, just Node 5.3. Is anyone else seeing this problem?

Sexycrets commented 8 years ago

@thomasbandit unfortunately not. I tried to run npm install and it did not work. I deleted the node_modules folder again and then run npm install and I still get the same error on any the package that I installed after upgrading to 0.18.

Same exact issue if I delete the node_modules and redo all the npm init and then the npm install

I even created a clean project from scratch with react-native init xxx, installed the package npm install -save app-module-path and required it in index.ios.js as require('app-module-path').addPath(__dirname);

I have closed the packager terminal window, xcode, cleaned up the cache, removed the temp files. No dice, still same exact error.

Basically from the 0.18 upgrade any module I add never works.

Does anybody know how to change the path where react-native looks for the modules? It seems it keep looking in the /Users/node_modules/module folder instead using the project specific one. Am I missing something?

MattFoley commented 8 years ago

Not sure if this solves anyone else's problem, but I eventually solved this by forcing my Jenkins instance to run npm install and npm cache clean via sudo as a normal user account OSX using nvm, rather than as the Jenkins user, or inside a Linux Docker instance.

Sexycrets commented 8 years ago

@MattFoley Thanks but did not work for me, I have a mac too.

zenlambda commented 8 years ago

I get this 'Invalid directory /User/node_modules/react' when trying to require 'react-router' in my react-native project. (I know there may be other reasons why react-router won't work with react-native, but I figured since #2985 was closed it was worth a shot).

Dev platform: OS X 10.11.2 Target platform: IOS 9.2 React Native version: 0.19.0 React-Router version: 2.0.0-rc5

Sexycrets commented 8 years ago

Tried to update to 0.19 and have same issue with any module I try to install (app-module-path in this case).

React Native version: 0.19.0 Dev platform: OS X 10.11.2 App-module-path: 1.0.5

lwansbrough commented 8 years ago

Unable to resolve module react-native from /appdir/index.ios.js: Invalid directory /Users/node_modules/react-native

jasonmerino commented 8 years ago

FWIW When I've run into this issue, and it's happened to me and a coworker a couple times, after clearing all the caches it sometimes helps to restart my computer. Also, when restarting my Mac and getting that Are you sure you want to restart dialog I needed to uncheck the Reopen windows when logging back in checkbox.

Sounds a bit extreme, but it's gotten me out of this issue a few times.

Sexycrets commented 8 years ago

@jasonmerino thank you, could you please elaborate on the specific procedure you used to "clear all caches" before rebooting?

Thank you very much !

jasonmerino commented 8 years ago

@Sexycrets clearing all caches was just referring to the steps in the very first post on this thread.

Sexycrets commented 8 years ago

@jasonmerino I did the following as you suggested:

  1. `rm -rf node_modules && npm install``
  2. rm -fr $TMPDIR/react-*
  3. node_modules/react-native/packager/packager.sh --reset-cache (the terminal hanged after [9:28:43 PM] <END> Building Dependency Graph (3365ms) so I killed it with control+C)
  4. watchman watch-del-all (returned { "version": "4.1.0", "roots": [ "/Users/MyUser/myfolder/MyProjectName" ] }
  5. shut down my mac unchecking the box to reopen open windows then turned it on again
  6. installed the package I want to use npm install --save app-module-path and it gets successfully installed: SexycretsMobile@0.0.1 /Users/FreeMako/sxs-mob/SexycretsMobile └── app-module-path@1.0.5
  7. required in my main.js: require('app-module-path').addPath(__dirname);
  8. opened the XCode project file under the iOS folder and clicked run
  9. build succeeds (suspiciously quickly), the packager runs in the terminal
  10. the simulator opens and I get the same error as before: screen shot 2016-02-01 at 9 33 33 pm

I guess at this point how can I remove react-native altogether as it was never been installed? So I can start fresh? I already uninstalled the react-native-cli and nothing changed, same after uninstalling and installing watchman.