From 82354640fff4d3be33f411c61bdbf09fe2af2ea3 Mon Sep 17 00:00:00 2001 From: aleclarsoniv Date: Tue, 20 Sep 2016 13:46:51 -0700 Subject: [PATCH] Fix node_modules resolution error messages Summary: We could probably relieve a lot of pain (in [this issue](https://github.com/facebook/react-native/issues/4968) specifically) by fixing the error message for when a module cannot be resolved after trying every relevant `node_modules` directory. Currently, you get a confusing error message that only gives you the last `node_modules` directory checked. By creating the error message where we can access the `searchQueue`, we're able to provide all of the attempted `node_modules` directories. Here's an example error message: ``` Unable to resolve module leftpad from /Users/aleclarson/ReactProject/src/stuff/index.js: Module does not exist in the module map or as these directories: /Users/aleclarson/ReactProject/src/stuff/node_modules/leftpad /Users/aleclarson/ReactProject/src/node_modules/leftpad /Users/aleclarson/ReactProject/node_modules/leftpad /Users/aleclarson/node_modules/leftpad /Users/node_modules/leftpad ``` Closes https://github.com/facebook/react-native/pull/9832 Differential Revision: D3895408 Pulled By: davidaurelio fbshipit-source-id: 872c9a3bb3633f751ec69b586a261616578ed511 --- .../DependencyGraph/ResolutionRequest.js | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js b/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js index b1fe2b93614..7a174c5e834 100644 --- a/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -375,11 +375,7 @@ class ResolutionRequest { } } - let p = Promise.reject(new UnableToResolveError( - fromModule, - toModuleName, - 'Node module not found', - )); + let p = Promise.reject(new UnableToResolveError(fromModule, toModuleName)); searchQueue.forEach(potentialModulePath => { p = this._tryResolve( () => this._tryResolve( @@ -390,7 +386,22 @@ class ResolutionRequest { ); }); - return p; + return p.catch(error => { + if (error.type !== 'UnableToResolveError') { + throw error; + } + throw new UnableToResolveError( + fromModule, + toModuleName, + `Module does not exist in the module map ${searchQueue.length ? 'or in these directories:' : ''}\n` + + searchQueue.map(searchPath => ` ${path.dirname(searchPath)}\n`) + '\n' + + `This might be related to https://github.com/facebook/react-native/issues/4968\n` + + `To resolve try the following:\n` + + ` 1. Clear watchman watches: \`watchman watch-del-all\`.\n` + + ` 2. Delete the \`node_modules\` folder: \`rm -rf node_modules && npm install\`.\n` + + ` 3. Reset packager cache: \`rm -fr $TMPDIR/react-*\` or \`npm start -- --reset-cache\`.` + ); + }); }); } } @@ -458,13 +469,7 @@ class ResolutionRequest { throw new UnableToResolveError( fromModule, toModule, -`Unable to find this module in its module map or any of the node_modules directories under ${potentialDirPath} and its parent directories - -This might be related to https://github.com/facebook/react-native/issues/4968 -To resolve try the following: - 1. Clear watchman watches: \`watchman watch-del-all\`. - 2. Delete the \`node_modules\` folder: \`rm -rf node_modules && npm install\`. - 3. Reset packager cache: \`rm -fr $TMPDIR/react-*\` or \`npm start -- --reset-cache\`.`, + `Directory ${potentialDirPath} doesnt exist`, ); }