106

The following is what I want to disable:

[js] File is a CommonJS module; it may be converted to an ES6 module.

I can't find it in settings.
Help appreciated as this is really annoying.

10 Answers 10

64

This is a new feature added in Visual Studio Code called "Suggestion Code Actions". "Suggestion Code Actions" are enabled by default in JavaScript and TypeScript.

You can disable them by setting: "typescript.suggestionActions.enabled": false or "javascript.suggestionActions.enabled": false in your user/workspace settings. The documentation can be found here.

(Image provided by Yusuf Yaşar.)

10
  • 76
    Is there any way to turn off only this warning and not all suggestions? Apr 6, 2018 at 14:25
  • 4
    Yep, that works, thank you, although it would be nice to only turn off that warning. Apr 7, 2018 at 14:32
  • 39
    Thread linked above ends up with "I don't understand why this is closed. The problem exists" and "I've locked this issue because +1 comments are causing people to miss that there is already is a solution to disable these hints.". I don't know what's wrong with Microsoft, or why they purposefully ignore to acknowledge that there's a problem.
    – Nato Boram
    Jul 29, 2018 at 4:26
  • 4
    in that thread nobody asked how to turn off suggestions only that turn off es6 import suggestions for nodejs files, eslint you define what module you want yet it can't read eslint config? Oct 23, 2018 at 18:58
  • 14
    Sadly this still seems to be an issue moving towards the end of 2020
    – apena
    Sep 30, 2020 at 17:22
49

For anyone using Vim with coc.nvim, you can make the same change by adding the same in the :CocConfig object:

"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:

{
    "javascript.suggestionActions.enabled": false
}
2
  • 1
    Doesn't this stop all suggestions related to JS ? Jan 19, 2022 at 17:56
  • @ParsaNoori yes it does, there isn't a more precise way of doing it unfortunately - see the comments in the accepted answer
    – icc97
    Jan 20, 2022 at 10:49
31

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:

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

You can also use the nvim-lsp-ts-utils plugin to filter out this specific diagnostic message while keeping suggestions enabled by adding this somewhere in your tsserver setup:

require('lspconfig').tsserver.setup({
    on_attach = function(client, bufnr)
        require('nvim-lsp-ts-utils').setup({
            filter_out_diagnostics_by_code = { 80001 },
        })
        require('nvim-lsp-ts-utils').setup_client(client)
    end,
})
5
  • I followed this but it's not working for me :/ I've contrasted this info with the official Lsp documentation and typescript-language-server docs and it seems to be right. However the message keeps appearing, don't know why.
    – Jaditpol
    Dec 27, 2021 at 21:06
  • Make sure you're not calling setup on tsserver more than once. You need to add the init_options block inside your existing setup call.
    – Cyhyraeth
    Dec 29, 2021 at 19:44
  • Thanks I double checked and the issue was that I was overwriting the on_attach property in the second call. Removed that and the message disappeared.
    – Jaditpol
    Jan 7, 2022 at 20:19
  • For what it's worth, I also was getting an issue with this not working. Turns out calling jose-elias-alvarez/typescript.nvim's automatically calls the LSP setup a second time, which overwrites the former call.
    – Alex W
    Jul 15, 2023 at 13:26
  • Relevant link for preferences.disableSuggestions: github.com/typescript-language-server/… Jan 1 at 6:43
18

Alert! This approach might be too much for VSCode users who love the intelligent coding assistance. Use it as a simple & quick help, together with other linting & testing utilities.


The control of the presence of the said message is located in Settings => Extensions => TypeScript. (TypeScript !!! :P)

As shown in screenshot, I searched in Settings with keyword "validate", then click TypeScript. It's the first item.

enter image description here

2
  • 20
    This is disabling all validation for javascript. Kind of a "throwing the baby out with the bathwater" situation. Jul 21, 2021 at 17:07
  • yes, it was too much for me ))
    – Azamat
    Dec 19, 2022 at 12:06
10

Actually this annoying suggestion comes from 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.

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.

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).

After you successfully compile your fork of TypeScript, then change your user settings.json to point to your vscode fork:

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

Done.

Restart your vscode, and the annoying suggestion has gone.

You can check this commit to see which source files of TypeScript need to be modify.

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.

2
  • 1
    It is so tedious and how can I upgrade?
    – Run
    Aug 9, 2021 at 3:43
  • @Run To upgrade, just pull the typescript repository and repeat the process again. But this is an outdated answer. Recent versions of TypeScript still have not fixed this issue?
    – weakish
    Aug 9, 2021 at 11:04
10

If your project is "type": "module" and you need to have a CommonJS file in it, e.g. 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. The fix for this has shipped with TypeScript 4.5.1 about a year ago.

1
  • 1
    Great answer, renaming .eslint.js to .cjs makes much more sense than turning suggestions in the editor! Jul 3, 2023 at 17:27
3

If you are getting this error in Next js, try the code below.

  1. Next.js includes the "next/babel" preset to your app, which includes everything needed to compile React applications and server-side code.

  2. Open your .eslintrc.json

    { "extends" : "next/babel" }
    
  3. But if you want to extend the default Babel configs, it's also possible. https://nextjs.org/docs/advanced-features/customizing-babel-config

1

For those using Neovim, you can ignore specifically just this diagnostic by overriding the LSP handler for publishDiagnostics.

I'm using Neovim v0.9.2 and lspconfig. If you're not using lspconfig, I think you can still override the LSP handler using vim.lsp.with().

require('lspconfig').typescript.setup {
    handlers = {
        ["textDocument/publishDiagnostics"] = function(_, result, ctx, config)
            if result.diagnostics ~= nil then
                local idx = 1
                while idx <= #result.diagnostics do
                    if result.diagnostics[idx].code == 80001 then
                        table.remove(result.diagnostics, idx)
                    else
                        idx = idx + 1
                    end
                end
            end
            vim.lsp.diagnostic.on_publish_diagnostics(_, result, ctx, config)
        end,
    }
}

I found this out from How to ignore tsserver error: "File is a commonjs module..." on r/neovim, but then adapted the code to work for my version of Neovim.

0

For Neovim Users

Much credit goes to this previous answer but it won't work if you're using the pmizio/typescript-tools.nvim plugin for the LSP support (and much more!). And if you're using the same plugin like me, you'll need the following slightly updated configurations:

require('typescript-tools).setup({
  settings = {
    tsserver_file_preferences = {
      disableSuggestions = true,
    },
  },
})

For more advanced configuration of your TypeScript development I strongly recommend checking out the repository which contains additional information.

-10

Maybe you don't have function(request, response), try it. It works for me

1
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
    – Community Bot
    Sep 7, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.