Skip to content

Commit fe3aebf

Browse files
MatthieuLemoinefacebook-github-bot
authored andcommittedMar 21, 2019
fix: Start Metro packager from project root (#24070)
Summary: Fixes #23342. Since Metro [v0.47](https://github.com/facebook/metro/releases/tag/v0.47.0), some babel plugins such as [babel-plugin-module-resolver](https://github.com/tleunen/babel-plugin-module-resolver) or [babel-plugin-import-graphql](https://github.com/detrohutt/babel-plugin-import-graphql) fail to resolve files if the packager is started through Xcode. They receive wrong filenames from Babel such as `${projectRoot}/node_modules/react-native/src/index.js` instead of `${projectRoot}/src/index.js`. It happens because the start command of the local-cli is not executed in the projectRoot directory. In this case, the cwd will be `${projectRoot}/node_modules/react-native`. This issue doesn't occur if you start the packager yourself using `node node_modules/react-native/local-cli/cli.js start` from your project root. It comes from this [line](https://github.com/facebook/react-native/blob/b640b6faf77f7af955e64bd03ae630ce2fb09627/scripts/packager.sh#L11). This script changed the working directory to `${projectRoot}/node_modules/react-native` and started Metro from there. Starting Metro from the project root fixes this issue. [iOS] [Fixed] - Start Metro packager from project root Pull Request resolved: #24070 Differential Revision: D14563996 Pulled By: cpojer fbshipit-source-id: cdeff34610f1ebb5fb7bc82a96f4ac9eec750d16
1 parent 7324555 commit fe3aebf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎scripts/packager.sh

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
# This source code is licensed under the MIT license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
# scripts directory
78
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
9+
REACT_NATIVE_ROOT="$THIS_DIR/.."
10+
# Application root directory - General use case: react-native is a dependency
11+
PROJECT_ROOT="$THIS_DIR/../../.."
812

913
# shellcheck source=/dev/null
1014
source "${THIS_DIR}/.packager.env"
11-
cd "$THIS_DIR/.." || exit
12-
node "./cli.js" start "$@"
15+
16+
# When running react-native tests, react-native doesn't live in node_modules but in the PROJECT_ROOT
17+
if [ ! -d "$PROJECT_ROOT/node_modules/react-native" ];
18+
then
19+
PROJECT_ROOT="$THIS_DIR/.."
20+
fi
21+
# Start packager from PROJECT_ROOT
22+
cd "$PROJECT_ROOT" || exit
23+
node "$REACT_NATIVE_ROOT/cli.js" start "$@"

0 commit comments

Comments
 (0)
Please sign in to comment.