Skip to content

Conversation

@jarrodldavis
Copy link

When editing JavaScript files in VS Code, TypeScript will suggest converting any CommonJS module to an ES6 module, even for node.js backend code. Unfortunately, how TypeScript determines when to suppress or show this suggestion is flawed, especially in a combination frontend/backend project like this one where the frontend code can use ES6 modules through transpilation.

This adds a jsconfig.json file to the starter files that will suppress the spurious suggestions for the node.js code, but maintain the suggestion in the frontend code.

See these GitHub issues and pull requests for more details on the issue:

When editing JavaScript files in VS Code, TypeScript will suggest
converting any CommonJS module to an ES6 module, even for node.js
backend code. Unfortunately, how TypeScript determines when to suppress
or show this suggestion is flawed, especially in a combination
frontend/backend project like this one where the frontend code can use
ES6 modules through transpilation.

This adds a `jsconfig.json` file to the starter files that will suppress
the spurious suggestions for the node.js code, but maintain the
suggestion in the frontend code.

See these GitHub issues and pull requests for more details on the issue:

- microsoft/vscode#47299
- microsoft/vscode#47458
- microsoft/TypeScript#23391
- microsoft/TypeScript#23576
- microsoft/TypeScript#25721

- microsoft/TypeScript#25721 (comment)
- microsoft/TypeScript#23391 (comment)
jarrodldavis added a commit to jarrodldavis/Learn-Node that referenced this pull request Jan 20, 2019
@mbodm
Copy link

mbodm commented Mar 5, 2020

There are so many comments on endless issues about this topic. And, in my oppinion, some of the most stupid answers are from the VS Code team itself. Like telling people they should deactivate all Suggested Actions for JS:

I've locked this issue because +1 comments are causing people to miss that there is already is a solution to disable these hints. Posting again to make it clear:

These are suggestions/hints. They visually indicate that vscode can perform an action to possibly refactor/improve your code, but they are not treated as errors.

You can disable them by adding "javascript.suggestionActions.enabled": false to your user settings

The only valid way, as of March 2020, to get that TS 80001 Quick Action message ("File is a CommonJS module; it may be converted to an ES6") out of the way, is what @jarrodldavis said (thx a lot for that):

By adding "noEmit": false to the compilerOptions section in the jsconfig.json file.

That said: It is nearly unbelievable that we still, in 2020, can make a new directory, put a single JS file with simple Node.js code into it, and immediately get that Suggested Action under the require() statement.

  • None of all the compiler options helps (target, module, moduleResolution).
  • Not that dubious module auto-detection, the VS Code team built into it, in version 1.22 or so.
  • No renaming of .js files into .cjs files.
  • Nothing, besides above solution (noEmit), which is totally unclear for ppl.

While the Node.js team still clearly states, that they fully support ES Modules but still flag them behind the --experimental-features flag, for good reasons.

So: How hard is it, for the VS Code team, to just fix such a f**in simple thing like that? After ALL that issues? In 2020? Even with a simple obvious compiler flag like "useCommonJsModules" or anything along those lines. Or link it at least to the moduleResolution or module option. Or use file endings like .cjs correctly. Or Whatever. EVERYTHING is better than letting ppl search the whole interwebz until they realize, after 4+ hours of searching, that for some weird reason the above solution is the only one that works.

That is unbelievable to me and over my head.

Sorry, i do not wanna sound like an asshole, VS Code is a great piece of software. But stuff like this should really work correct out of the box, in 2020. After so many years and issues now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants