Description
Environment
System:
OS: macOS 11.6
CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
Memory: 983.04 MB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
Yarn: 1.22.11 - ~/.nvm/versions/node/v14.16.0/bin/yarn
npm: 7.19.1 - ~/.nvm/versions/node/v14.16.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.10.1 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 15.0, DriverKit 20.4, macOS 11.3, tvOS 15.0, watchOS 8.0
Android SDK:
Android NDK: 21.0.6113669
IDEs:
Android Studio: 2020.3 AI-203.7717.56.2031.7784292
Xcode: 13.0/13A233 - /usr/bin/xcodebuild
Languages:
Java: 11.0.12 - /usr/local/opt/openjdk@11/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: Not Found
react-native: Not Found
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
Description
I want to react-native init
a new app while my current working directory is a (a sub-directory of) another package.
But when I do this from within a package that has not yet been installed (or linked in the case of a Lerna mono-repo), the CLI emits confusing warnings (see example below).
I believe the root cause of these warnings is that an attempt is made to locate a root package and load its configuration (https://github.com/react-native-community/cli/blob/master/packages/cli/src/index.ts#L217) independently on what command is being executed.
The default behaviour of loadConfig
is to find the nearest package.json
up the tree: https://github.com/react-native-community/cli/blob/master/packages/cli/src/tools/config/index.ts#L61 which will find the enclosing package with uninstalled dependencies.
Ideally running react-native init
shouldn't traverse my file system to determine a configuration.
Most likely the commands meant to be executed "detached" from a package, should have an early return code-path to make sure the configuration is never loaded when running those.
Reproducible Demo
# Create a package
cat << EOF > package.json
{
"name": "wrong-root-package",
"dependencies": {
"unresolvable-package": "^1.0.0"
}
}
EOF
# Try running the "react-native" CLI to initialize a new app
npx --yes react-native init MyTestApp
When running this, notice the warning emitted from the CLI:
warn Package unresolvable-package has been ignored because it contains invalid configuration. Reason: Cannot find module 'unresolvable-package/package.json'
Require stack:
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/tools/config/resolveNodeModuleDir.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/tools/releaseChecker/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/start/runServer.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/start/start.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/bin.js
Workaround
Users can place a package.json
containing an empty object ({}
) in the current working directory before executing the react-native init
command.