简体   繁体   English

如何禁用:[js] 文件是 CommonJS 模块; 它可以转换为 ES6 模块

[英]How do I disable: [js] File is a CommonJS module; it may be converted to an ES6 module

The following is what I want to disable:以下是我要禁用的内容:

[js] 文件是一个 CommonJS 模块;它可以转换为 ES6 模块。

I can't find it in settings.我在设置里找不到。
Help appreciated as this is really annoying.帮助表示赞赏,因为这真的很烦人。

This is a new feature added in Visual Studio Code called "Suggestion Code Actions".这是 Visual Studio Code 中添加的一项新功能,称为“建议代码操作”。 "Suggestion Code Actions" are enabled by default in JavaScript and TypeScript. “建议代码操作”在 JavaScript 和 TypeScript 中默认启用。

You can disable them by setting: "typescript.suggestionActions.enabled": false or "javascript.suggestionActions.enabled": false in your user/workspace settings.您可以通过在您的用户/工作区设置中设置: "typescript.suggestionActions.enabled": false"javascript.suggestionActions.enabled": false来禁用它们。 The documentation can be found here .文档可以在这里找到。

(Image provided by Yusuf Yaşar .) (图片由Yusuf Yaşar提供。)

For anyone using Vim with coc.nvim , you can make the same change by adding the same in the :CocConfig object:对于使用带有coc.nvim 的Vim 的任何人,您可以通过在:CocConfig对象中添加相同的内容来进行相同的更改:

"javascript.suggestionActions.enabled": false

If you haven't added any settings to :CocConfig before, then you need to make sure the above setting is wrapped in a JSON object:如果您之前没有向:CocConfig添加任何设置,那么您需要确保将上述设置包装在一个 JSON 对象中:

{
    "javascript.suggestionActions.enabled": false
}

Actually this annoying suggestion comes from TypeScript.实际上,这个烦人的建议来自 TypeScript。

Thus, to turn off this suggestion, you can modify the source code of TypeScript, compile it, then tell vscode to use your fork of TypeScript.因此,要关闭此建议,您可以修改 TypeScript 的源代码,编译它,然后告诉 vscode 使用您的 TypeScript 分支。

As a quick and dirty hack, just remove the logic related to ts.Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module , then compile the project following the instructions on TypeScript's README.作为一个快速而肮脏的黑客,只需删除与ts.Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module相关的逻辑,然后按照 TypeScript 的 README 中的说明编译项目。

The compilation will fail because removing the related logic causes some functions become unused, then you just remove those unused function definitions and recompile the project ( gulp clean && gulp local ).编译将失败,因为删除相关逻辑会导致一些函数变得不使用,然后您只需删除那些未使用的函数定义并重新编译项目( gulp clean && gulp local )。

After you successfully compile your fork of TypeScript, then change your user settings.json to point to your vscode fork:成功编译 TypeScript 的 fork 后,将用户 settings.json 更改为指向您的 vscode fork:

"typescript.tsdk": "/path/to/your/fork/of/TypeScript/built/local",

Done.完毕。

Restart your vscode, and the annoying suggestion has gone.重新启动你的 vscode,烦人的建议就消失了。

You can check this commit to see which source files of TypeScript need to be modify.您可以检查此提交以查看需要修改哪些 TypeScript 源文件。

Warn: the modification is quick and dirty, use them at your own risk.警告:修改既快速又脏,使用它们的风险自负。 If you find anything wrong, you can just remove the tsdk configuration, to switch back to vscode's built-in TypeScript.如果你发现任何错误,你可以删除 tsdk 配置,切换回 vscode 的内置 TypeScript。

Alert!警报! This approach might be too much for VSCode users who love the intelligent coding assistance.对于喜欢智能编码帮助的 VSCode 用户来说,这种方法可能太过分了。 Use it as a simple & quick help, together with other linting & testing utilities.将它与其他 linting 和测试实用程序一起用作简单快速的帮助。


The control of the presence of the said message is located in Settings => Extensions => TypeScript .所述消息存在的控制位于Settings => Extensions => TypeScript (TypeScript !!! :P) (打字稿!!!:P)

As shown in screenshot, I searched in Settings with keyword "validate", then click TypeScript.如屏幕截图所示,我在“设置”中使用关键字“验证”进行搜索,然后单击 TypeScript。 It's the first item.这是第一项。

在此处输入图片说明

Add the .vscode file into the project file path. 将.vscode文件添加到项目文件路径。 Insert the settings.json file into the .vscode file. 将settings.json文件插入.vscode文件。

在此处输入图片说明

For anyone using Neovim with Native LSP and nvim-lspconfig for setting up your language servers, you can disable suggestions by adding this somewhere in your tsserver setup:对于使用带有 Native LSP 和nvim-lspconfig来设置语言服务器的任何人,您可以通过在 tsserver 设置中的某处添加以下内容来禁用建议:

require('lspconfig').tsserver.setup {
    init_options = {
        preferences = {
            disableSuggestions = true
        }
    }
}

If you are getting this error in Next js, try the code below.如果您在 Next js 中遇到此错误,请尝试以下代码。 1- Next.js includes the "next/babel" preset to your app, which includes everything needed to compile React applications and server-side code. 1- Next.js 包含“next/babel”预设到您的应用程序,其中包括编译 React 应用程序和服务器端代码所需的一切。 2- Open your .eslintrc.json 2- 打开你的 .eslintrc.json

{ "extends" : "next/babel" }

3-But if you want to extend the default Babel configs, it's also possible. 3-但是如果你想扩展默认的 Babel 配置,它也是可能的。 https://nextjs.org/docs/advanced-features/customizing-babel-config https://nextjs.org/docs/advanced-features/customizing-babel-config

If your project is "type": "module" and you need to have a CommonJS file in it, eg to configure ESLint (which doesn't support ESM as of writing this), then just rename it from *.js to *.cjs (or from *.ts to *.cts , if appropriate), and the suggestion will go away.如果你的项目是"type": "module"并且你需要在其中有一个 CommonJS 文件,例如配置 ESLint(在撰写本文时它不支持 ESM),那么只需将其从*.js重命名为 *.js 即可*.cjs (或从*.ts*.cts ,如果合适的话),并且建议将 go 消失。 The fix for this has shipped with TypeScript 4.5.1 about a year ago .大约一年前,此修复程序与 TypeScript 4.5.1 一起提供

也许你没有“功能(请求,响应)”试试看,它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM