Skip to content

Commit e3c45c4

Browse files
authored
Documentation: debug out of project directory (#1980)
* Update documentation: debug out of project directory * Update
1 parent 3486c4f commit e3c45c4

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Using this extension, you can **debug your code and quickly run `react-native` c
3030
- [About the extension](#about-the-extension)
3131
- [Getting started](#getting-started)
3232
- [React Native commands in the Command Palette](#react-native-commands-in-the-command-palette)
33+
- [Customize metro configuration](#customize-metro-configuration)
3334
- [Debugging React Native applications](#debugging-react-native-applications)
3435
- [Hermes engine](#hermes-engine)
3536
- [iOS applications](#ios-applications)
@@ -46,6 +47,7 @@ Using this extension, you can **debug your code and quickly run `react-native` c
4647
- [Windows Hermes debugging](#windows-hermes-debugging)
4748
- [macOS applications](#react-native-for-macos)
4849
- [macOS Hermes debugging](#macos-hermes-debugging)
50+
- [Debug out of React Native project directory](#debug-out-of-react-native-project-directory)
4951
- [TypeScript and Haul based applications](#typescript-and-haul)
5052
- [Debugger configuration properties](#debugger-configuration-properties)
5153
- [Customization](#customization)
@@ -135,6 +137,12 @@ To run React Native Tools commands via VS Code tasks, you can create a `.vscode/
135137
}
136138
```
137139

140+
# Customize metro configuration
141+
142+
Metro is a JavaScript bundler for React Native and include in React Native package. Metro configuration can be customized in `metro.config.js`.
143+
144+
Note: From React Native 0.72.0, the config loading setup for Metro in React Native CLI(`@react-native/metro-config`).
145+
138146
# Debugging React Native applications
139147

140148
To start debugging create a new debug configuration for your ReactNative app in your `.vscode/launch.json`. Adding a new configuration can be done by opening your `launch.json` file and clicking on `Add Configuration...` button and then selecting `React Native` option. After that the extension will prompt you to create a debugging configuration by selecting debugging parameters in dropdown lists at the top of the editor. A new debugging configuration will be generated and added to the `launch.json` file automatically as shown in the image below. For Expo projects, please make sure choose `Application in direct mode(Hermes)` if you are using SDK 48 or a newer SDK.
@@ -497,6 +505,67 @@ To debug a macOS Hermes application you can use `Debug macOS Hermes - Experiment
497505
}
498506
```
499507

508+
## Debug out of React Native project directory
509+
510+
If your project structure like this:
511+
512+
```
513+
common
514+
- utils.ts
515+
app
516+
- src/
517+
- metro.config.js
518+
```
519+
520+
When you import `utils.ts` in your project. Using
521+
522+
```js
523+
import { commonFunction } from "../../common/utils";
524+
```
525+
526+
Will get error when start Metro:
527+
528+
```
529+
error: bundling failed: Error: Unable to resolve module `../../common/utils` from `src/App.js`
530+
```
531+
532+
To import files in `metro.config.js`, user can debug code out of react native project.
533+
534+
1. Add extra module and watch folder for the file parent folder.
535+
536+
```js
537+
const extraNodeModules = {
538+
common: path.resolve(__dirname + "/../common"),
539+
};
540+
const watchFolders = [path.resolve(__dirname + "/../common")];
541+
```
542+
543+
2. Add module and watch folder in metro config.
544+
545+
```js
546+
// React native <= 0.72.0
547+
module.exports = {
548+
resolver: {
549+
extraNodeModules,
550+
},
551+
watchFolders,
552+
};
553+
554+
// React native >= 0.72.0
555+
const config = {
556+
resolver: {
557+
extraNodeModules,
558+
},
559+
watchFolders,
560+
};
561+
```
562+
563+
3. After mapping common key to common/ path, we can include any files inside common/ relative to this path. Metro is started, launching or debugging is working well.
564+
565+
```js
566+
import { commonFunction } from "common/utils";
567+
```
568+
500569
## TypeScript and Haul
501570

502571
### Sourcemaps
@@ -726,14 +795,25 @@ If you use Android, you need to change the debug server by:
726795
5. (Hermes only) Hermes engine listens port 8081 for debugging by default, to change it you might need to modify your [`metro.config.js` file adding `"port": portNumber` argument in there to the server settings](https://facebook.github.io/metro/docs/configuration/#port).
727796

728797
```js
729-
// Example of metro.config.js
798+
// Example of metro.config.js (<= 0.72.0)
730799
module.exports = {
731800
server: {
732801
port: 9091,
733802
},
734803
};
735804
```
736805

806+
OR
807+
808+
```js
809+
// Example of metro.config.js (0.72.0)
810+
const config = {
811+
server: {
812+
port: 9091,
813+
},
814+
};
815+
```
816+
737817
<details>
738818
<summary>Port setup instruction</summary>
739819

0 commit comments

Comments
 (0)