63

Visual Studio Code now supports JSX on 0.8 version, but looks like the only way to activate it is with a .jsx file extension. It is not on the list to change the language mode manually, the nearest option is JavaScriptReact, but it doesn't parse the JSX tags.

I'm in a project with a lot of .js files with JSX and I can't change it.

Is there any other way to use JSX syntax without the .jsx extension?

Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
William Grasel
  • 1,181
  • 1
  • 10
  • 11

10 Answers10

158

Change your user settings or workspace settings as below:

// Place your settings in this file to overwrite the default settings
{
    "files.associations": {
        "*.js": "javascriptreact"
    }
}

Note: You might need to restart VSCode.

Belfield
  • 1,399
  • 1
  • 12
  • 18
Dionys
  • 3,083
  • 1
  • 19
  • 28
11

I would feel the below is the easiest way of formatting the code

Click on the bottom right on VS Code Editor where it says Javascript.

You will see an option to Select the language Mode, here you can search for JavaScriptReact and select. That's it. This should solve your problem.

1.Check if JavascriptReact is selected?? enter image description here

Abhilash Reddy
  • 1,519
  • 1
  • 10
  • 11
  • 1
    This question came from a time were this option did not existed yet. Right now it is as simple as you said. – William Grasel Aug 15 '19 at 20:26
  • Why don't I see this at the bottom of my vs editor? I have the latest update of vs community and the latest version of react installed. and all extensions updated. Instead I see a line showing one compile error and left/right arrow to go through errors and an 'add to source control github line in blue. – Andrew H Jun 14 '20 at 20:01
  • It worked for me, but why simple Javascript did not work? – Tanvi Agarwal Jan 07 '23 at 12:59
7

I could do it, but "not React JS files" are also show with JavaScriptReact mode.

  1. open file C:\Program Files (x86)\Microsoft VS Code\resources\app\plugins\vs.language.javascript\syntaxes\javascriptreact.json (probably, need to open with administrator privileges.)
  2. change "jsx" to "js" in array "fileTypes".
  3. restart app, close opened js files, and reopen.
Kohei Sugimura
  • 326
  • 1
  • 10
  • 4
    For anyone else wondering, on Mac the path is: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/javascript/syntaxes/JavaScriptReact.tmLanguage – Mike Lentini Nov 10 '15 at 00:52
  • 1
    It worked for me. on Mac, added "js" also to the array list in this file: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/javascript/syntaxes/JavaScriptReact.t‌​mLanguage ` jsx js ` – Jyo Reddy Nov 23 '15 at 04:48
  • 1
    for Version 0.10.1 on Windows10, file to edit is changed. `C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\javascript\package.json`. and add `".js"` to `contributes.languages[0].extensions`. – Kohei Sugimura Nov 24 '15 at 00:24
  • @KoheiSugimura your solution worked but I also had to remove .js from contributes.languages[1].extensions and then it worked! – Rocklan Dec 14 '15 at 00:51
6

Took me a while to figure this out but – JSX is already part of Emmet – which is part of VS Code. I've told Emmet to also (additionally) make JSX snippets available in regular JS files.

Just put this in your settings file:

"emmet.syntaxProfiles": {
    "javascript": "jsx"
}    
dbochen
  • 182
  • 1
  • 4
6

Although Dionys' answer works there is a better way to do this in more recent versions of Visual Studio Code.

Go to File>Prefrences>Settings and then scroll down and find "Emmet" open the tab and you should see the following text

  // Enable Emmet abbreviations in languages that are not supported by default. Add a 
  mapping here between the language and emmet supported language.
  //  E.g.: {"vue-html": "html", "javascript": "javascriptreact"}
  "emmet.includeLanguages": {},

So just follow the instructions and add "emmet.includeLanguages": { "javascript": "javascriptreact" } on the json at the right panel ( which will overwrite the user settings ).

Panos
  • 1,764
  • 21
  • 23
3

There is now a VS Code extension that allows .js files to be treated as .jsx files.

Unfortunately the readme for the extension also warns:

when you install this extension you will loose all the existing language support provided for .js files

Fortunately VS Code is now very close to adopting Salsa, which means soon the js-is-jsx issue should be completely resolved.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
1

Click on the bottom right on VS Code Editor where it says Javascript. You will see an option to Select the language Mode, here you can search for JavaScriptReact and select. That's it.

amir mohseni
  • 274
  • 2
  • 6
0

1-Press F1 (in Visual Studio Code)

2-Type "extension" in the appearing text field

3-Pick "Extensions: Install Extension"

3-Type "ext install jsx"

4- install JS JSX Snippets

5-Restart Visual Studio Code

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
-2

Just install an extension:

  • Press F1 (in Visual Studio Code)
  • Type "extension" in the appearing text field
  • Pick "Extensions: Install Extension"
  • Type "ext install jsx"
  • Restart Visual Studio Code

Source:

https://code.visualstudio.com/docs/editor/extension-gallery?pub=TwentyChung&ext=jsx https://marketplace.visualstudio.com/items/TwentyChung.jsx

Blank
  • 4,872
  • 2
  • 28
  • 25
  • 1
    Does this work for *.js files? I tried installing this and it doesn't seem to help with errors on the xml style stuff. – sethreidnz Dec 10 '15 at 01:18
  • 2
    This extension appears to be a wrapper around a TextMate JSX.tmLanguage file. As the person who posted the question already pointed out, VSCode already supports highlighting JSX, they just need to do so on a JS file rather than only JSX. Therefore, installing this extension may not be helpful. – djabraham Dec 12 '15 at 06:54
  • Yes this extension does not actually do anything as far as I can tell. Also I cannot figure out how to contact the guy who made it. I'd love to use vsCode for my react dev (webstorm sucks in comparison IMO)... Any one else got any good ideas? – sethreidnz Dec 15 '15 at 19:49
-6

Try using link on Mac or Linux.

ln -s index.ios.js index.ios.jsx
Bill Wong
  • 37
  • 1
  • 7