vscode imports import console = require("console"); automatically

import console = require("console");

console. << I type . and above gets imported automatically in VScode. Anybody knows how to disable that?

(I assume it is one of my extensions. Probably Prettier.)

edit: it only happens in React Typescript environment. not in Typescript without react.


Solution 1:

Disclaimer: this shouldn't be considered "the solution" but it's the simplest/fastest.

This answer is assuming you're using VSCode. Other IDEs should be similar.

  1. Start typing console
  2. Click enter or type ., allowing IntelliSense to add import console = require("console");
  3. Ctrl+click (or F12, or Cmd+click on macOS) on require("console")
  4. Comment out this code:
declare module "console" {
    export = console;
}

Solution 2:

I experienced this as well an it seems to be a problem with the Auto Import feature in VSCode. Disabling all extensions doesn´t seem to make it go away either.

As a workaround you can disable autoimports in settings.

If you use Javascript

"javascript.suggest.autoImports": false

If you use Typescript

"typescript.suggest.autoImports": false

enter image description here

EDIT: The faulty autoimport occurs because of this code in a package down the dependency tree

declare module "console" {
    export = console;
}

The package can be located in either your local node_modules directory or in a referenced package installed globally.

  1. Search your local node_modules for declare module "console"
  2. If you find it in a local package, run npm list [packageName] to determine which package in package.json is dependent on the package with the console code in it.

If you don´t find code in your local node_modules you could either

  1. Eliminate packages one by one in package.json

  2. Search for the console code in globally installed modules which may be referenced by packages in your project

%USERPROFILE%\AppData\Roaming\npm\node_modules %USERPROFILE%\AppData\Local\Microsoft\TypeScript

I know it´s not a straight forward solution but I hope it helps, in my case I had a reference from react-native-copilot -> rimraf -> node which had the console code in it. Removing react-native-copilot solved the problem.