ChatGPT解决这个技术问题 Extra ChatGPT

Webstorm not recognising JavaScript file

https://i.stack.imgur.com/wcd0U.png

Webstorm doesn't recognize one of my .js files as a JavaScript file so I'm losing syntax highlighting and being able to add break points. I've looked in the workspace.xml file and the file in question seems to have similar settings to other .js files that work correctly.

If I change the name of the file it works ok. So somehow Webstorm is stuck on misinterpreting the type of a file that has this name. Where else can I edit the project?

https://i.stack.imgur.com/knN72.png

may be broken indexes — try to run 'File -> Invalidate Caches...'
I did invalidate+restart but no change unfortunately.
Do you have the action 'Mark as JavaScript' in context menu for the file?
I've added the context to the question. Its big!
None of the answers so far worked for me. I renamed the file and it was recognized as JS.

a
anstarovoyt

So I see three possible reasons for the problem:

The file was marked as 'Plain text' There is a pattern for 'Text files' file type that matches this file (or back: file type 'JavaScript' exclude this file name). See image below

https://i.stack.imgur.com/uI0iE.png

There is a custom plugin that overrides default behavior for files with this name (unlikely)

Updated: after several years I've found one more reason for the behavior and most likely it the main source of the issues. When you create a file without any extensions the "Register New File Type Association" is appeared. And you can accidentally specify a new file type, for some file name. It is can be fixed with (2) but it is the reason why the pattern was added there.

https://i.stack.imgur.com/6kDBl.png

Note: the solution works for all IDEA-based IDEs: IntelliJ IDEA, WebStorm, RubyMine, PyCharm, PhpStorm.


I think 1) is most likely because I messed up creating it as a js file to begin with. How do I mark it correctly?
@IanWarburton It's #2 for sure -- #1 will have slightly different file icon. Especially considering your latest comment "I messed up creating it as a js file to begin with"
This is fairly easy to do. If you 'New' a generic file and forget to put the extension in the dialog it switches to defining a pattern. If you then type the extension in you've defined a new kind of text file!
Note that this also applies to IntelliJ IDEA (see youtrack.jetbrains.com/issue/WEB-19160)
this was the answer for me. i had created a new file without an extension, and when prompted on the file type in the next dialog i added an extension instead of choosing one from the list. at that point intellij "helpfully" added my new file to the Text files list :/
u
user2174102

File -> Settings -> Editor -> FileTypes -> Text files -> check for "ContentRepository.js" pattern there and remove it.


s
shashi

If the file was marked as "Plain Text", then this can be fixed as follows:

Right click on the file. One of the menu options is "Mark as Javascript" . Just below "Delete". Click that. Your file is now recognized as javascript by WebStorm.

Since WebStorm was not allowing screenshots after right click so couldn't add it here.


M
Mathias Gheno Azzolini

My problem was with Auto-detect file type by context

https://i.stack.imgur.com/1417M.png


On Ubuntu the tag is called 'File type auto-detected by content'
Y
Yash Sharma

For me this did the trick
1) Preferences
2) Editor
3) FileTypes
4) Search for javascript
5) Add *.js to registered patterns


R
Robert Sinclair

https://i.stack.imgur.com/TvD0I.png

For a block of code you can also use "language injection". Simply high-light the block of code, wait for the little lamp to appear then select the bottom choice and then the language you need (screenshot also attached)

https://i.stack.imgur.com/21Nnm.png


k
kingPuppy

In case it is helpful elsewhere (OMG this was scary).

For some reason IntelliJ was not recognizing any .js file in my project (even after .idea removal (rm -rf .idea)).

I went to Webstorm (in my case RubyMine): Webstorm > Preferences ... > Editor > File Types then went to Recognized Filed Types, picked Javascript (but whatever file type you are missing will do), then added back a Registered Patterns entry of *.js

Of course, press Apply and then Ok.

Wow, scary stuff. but solved my problem, hope it does for someone else.


D
DuDa

I was always trying to add .js as a recognised file types, but somehow .js was added to Ignored Files and Folders.

I just removed .js from Ignored Files and Folders and it worked for me.

Preferences Editor FileTypes Ignored Files and Folders Remove *.js

https://i.stack.imgur.com/vP5Dc.png


A
Aleksei

https://i.stack.imgur.com/kpbRg.png

2022 update!

Right click on file and choose JavaScript