|
| 1 | +--- |
| 2 | +id: setting-up-your-editor |
| 3 | +title: Setting up your Editor |
| 4 | +sidebar_label: Setting up your Editor |
| 5 | +--- |
| 6 | + |
| 7 | +## Syntax highlighting |
| 8 | + |
| 9 | +To configure the syntax highlighting in your favorite text editor, head to the [relevant Babel documentation page](https://babeljs.io/docs/editors) and follow the instructions. Some of the most popular editors are covered. |
| 10 | + |
| 11 | +## Displaying Lint Output in the Editor |
| 12 | + |
| 13 | +> Note: this feature is available with `[email protected]` and higher.<br> |
| 14 | +> It also only works with npm 3 or higher. |
| 15 | +
|
| 16 | +Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint. |
| 17 | + |
| 18 | +They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do. |
| 19 | + |
| 20 | +You would need to install an ESLint plugin for your editor first. Then, add a file called `.eslintrc` to the project root: |
| 21 | + |
| 22 | +```js |
| 23 | +{ |
| 24 | + "extends": "react-app" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +Now your editor should report the linting warnings. |
| 29 | + |
| 30 | +Note that even if you edit your `.eslintrc` file further, these changes will **only affect the editor integration**. They won’t affect the terminal and in-browser lint output. This is because Create React App intentionally provides a minimal set of rules that find common mistakes. |
| 31 | + |
| 32 | +If you want to enforce a coding style for your project, consider using [Prettier](https://github.com/jlongster/prettier) instead of ESLint style rules. |
| 33 | + |
| 34 | +## Debugging in the Editor |
| 35 | + |
| 36 | +**This feature is currently only supported by [Visual Studio Code](https://code.visualstudio.com) and [WebStorm](https://www.jetbrains.com/webstorm/).** |
| 37 | + |
| 38 | +Visual Studio Code and WebStorm support debugging out of the box with Create React App. This enables you as a developer to write and debug your React code without leaving the editor, and most importantly it enables you to have a continuous development workflow, where context switching is minimal, as you don’t have to switch between tools. |
| 39 | + |
| 40 | +### Visual Studio Code |
| 41 | + |
| 42 | +You would need to have the latest version of [VS Code](https://code.visualstudio.com) and VS Code [Chrome Debugger Extension](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome) installed. |
| 43 | + |
| 44 | +Then add the block below to your `launch.json` file and put it inside the `.vscode` folder in your app’s root directory. |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "version": "0.2.0", |
| 49 | + "configurations": [ |
| 50 | + { |
| 51 | + "name": "Chrome", |
| 52 | + "type": "chrome", |
| 53 | + "request": "launch", |
| 54 | + "url": "http://localhost:3000", |
| 55 | + "webRoot": "${workspaceRoot}/src", |
| 56 | + "sourceMapPathOverrides": { |
| 57 | + "webpack:///src/*": "${webRoot}/*" |
| 58 | + } |
| 59 | + } |
| 60 | + ] |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +> Note: the URL may be different if you've made adjustments via the [HOST or PORT environment variables](/docs/advanced-configuration). |
| 65 | +
|
| 66 | +Start your app by running `npm start`, and start debugging in VS Code by pressing `F5` or by clicking the green debug icon. You can now write code, set breakpoints, make changes to the code, and debug your newly modified code—all from your editor. |
| 67 | + |
| 68 | +Having problems with VS Code Debugging? Please see their [troubleshooting guide](https://github.com/Microsoft/vscode-chrome-debug/blob/master/README.md#troubleshooting). |
| 69 | + |
| 70 | +### WebStorm |
| 71 | + |
| 72 | +You would need to have [WebStorm](https://www.jetbrains.com/webstorm/) and [JetBrains IDE Support](https://chrome.google.com/webstore/detail/jetbrains-ide-support/hmhgeddbohgjknpmjagkdomcpobmllji) Chrome extension installed. |
| 73 | + |
| 74 | +In the WebStorm menu `Run` select `Edit Configurations...`. Then click `+` and select `JavaScript Debug`. Paste `http://localhost:3000` into the URL field and save the configuration. |
| 75 | + |
| 76 | +> Note: the URL may be different if you've made adjustments via the [HOST or PORT environment variables](/docs/advanced-configuration). |
| 77 | +
|
| 78 | +Start your app by running `npm start`, then press `^D` on macOS or `F9` on Windows and Linux or click the green debug icon to start debugging in WebStorm. |
| 79 | + |
| 80 | +The same way you can debug your application in IntelliJ IDEA Ultimate, PhpStorm, PyCharm Pro, and RubyMine. |
| 81 | + |
| 82 | +## Formatting Code Automatically |
| 83 | + |
| 84 | +Prettier is an opinionated code formatter with support for JavaScript, CSS and JSON. With Prettier you can format the code you write automatically to ensure a code style within your project. See the [Prettier's GitHub page](https://github.com/prettier/prettier) for more information, and look at this [page to see it in action](https://prettier.github.io/prettier/). |
| 85 | + |
| 86 | +To format our code whenever we make a commit in git, we need to install the following dependencies: |
| 87 | + |
| 88 | +```sh |
| 89 | +npm install --save husky lint-staged prettier |
| 90 | +``` |
| 91 | + |
| 92 | +Alternatively you may use `yarn`: |
| 93 | + |
| 94 | +```sh |
| 95 | +yarn add husky lint-staged prettier |
| 96 | +``` |
| 97 | + |
| 98 | +- `husky` makes it easy to use githooks as if they are npm scripts. |
| 99 | +- `lint-staged` allows us to run scripts on staged files in git. See this [blog post about lint-staged to learn more about it](https://medium.com/@okonetchnikov/make-linting-great-again-f3890e1ad6b8). |
| 100 | +- `prettier` is the JavaScript formatter we will run before commits. |
| 101 | + |
| 102 | +Now we can make sure every file is formatted correctly by adding a few lines to the `package.json` in the project root. |
| 103 | + |
| 104 | +Add the following field to the `package.json` section: |
| 105 | + |
| 106 | +```diff |
| 107 | ++ "husky": { |
| 108 | ++ "hooks": { |
| 109 | ++ "pre-commit": "lint-staged" |
| 110 | ++ } |
| 111 | ++ } |
| 112 | +``` |
| 113 | + |
| 114 | +Next we add a 'lint-staged' field to the `package.json`, for example: |
| 115 | + |
| 116 | +```diff |
| 117 | + "dependencies": { |
| 118 | + // ... |
| 119 | + }, |
| 120 | ++ "lint-staged": { |
| 121 | ++ "src/**/*.{js,jsx,json,css}": [ |
| 122 | ++ "prettier --single-quote --write", |
| 123 | ++ "git add" |
| 124 | ++ ] |
| 125 | ++ }, |
| 126 | + "scripts": { |
| 127 | +``` |
| 128 | + |
| 129 | +Now, whenever you make a commit, Prettier will format the changed files automatically. You can also run `./node_modules/.bin/prettier --single-quote --write "src/**/*.{js,jsx}"` to format your entire project for the first time. |
| 130 | + |
| 131 | +Next you might want to integrate Prettier in your favorite editor. Read the section on [Editor Integration](https://prettier.io/docs/en/editors.html) on the Prettier GitHub page. |
0 commit comments