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

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

Open satya164 opened this issue 10 years ago • 498 comments

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,

  • Delete the node_modules folder - rm -rf node_modules && npm install
  • Reset packager cache - rm -fr $TMPDIR/react-* or node_modules/react-native/packager/packager.sh --reset-cache
  • Clear watchman watches - watchman watch-del-all
  • Recreate the project from scratch

satya164 avatar Dec 24 '15 18:12 satya164

cc @amasad

brentvatne avatar Dec 24 '15 22:12 brentvatne

cc @martinbigio @davidaurelio @cpojer

amasad avatar Dec 25 '15 00:12 amasad

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

amasad avatar Dec 25 '15 00:12 amasad

yes, we met the same problem.

hufeng avatar Dec 25 '15 03:12 hufeng

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

martinbigio avatar Dec 25 '15 03:12 martinbigio

Same problem here on version 0.17.0.

$ watchman watch-list
{
    "version": "3.8.0",
    "roots": []
}

Hybrid-Force avatar Dec 25 '15 07:12 Hybrid-Force

I also got this problem when requesting for platform=ios

arypurnomoz avatar Dec 25 '15 11:12 arypurnomoz

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

udnisap avatar Dec 25 '15 15:12 udnisap

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

sgonyea avatar Dec 25 '15 21:12 sgonyea

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?

udnisap avatar Dec 26 '15 03:12 udnisap

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

sgonyea avatar Dec 26 '15 06:12 sgonyea

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?

amasad avatar Dec 26 '15 09:12 amasad

@amasad No, I am using npm v2.14.7

Hybrid-Force avatar Dec 26 '15 10:12 Hybrid-Force

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

  • watchman watch-del-all
  • rm -rf $TMPDIR/react-packager-*

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

pcottle avatar Dec 27 '15 00:12 pcottle

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

skevy avatar Dec 28 '15 04:12 skevy

@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

pcottle avatar Dec 28 '15 07:12 pcottle

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.

philikon avatar Dec 28 '15 21:12 philikon

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.

jukkatupamaki avatar Dec 30 '15 09:12 jukkatupamaki

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:

  • watchman watch-del-all
  • npm cache clean && npm install

Someone wants to send a PR for this? ;)

martinbigio avatar Dec 30 '15 22:12 martinbigio

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

udnisap avatar Dec 31 '15 03:12 udnisap