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

3
  • any luck with this? I am having the same issue Commented Nov 20, 2018 at 20:55
  • Besides the workarounds in the answers it seems that this is a bug in VSCode introduced with the last update. A corresponding bug-report is filled here: github.com/Microsoft/vscode/issues/70157
    – ofhouse
    Commented Mar 16, 2019 at 17:37
  • 2
    The TS/JS Nightly Plugin fixes this for VSCode 1.36+
    – Andrew Li
    Commented Jul 18, 2019 at 15:01

7 Answers 7

135

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;
}
5
  • 10
    If this were Reddit, I'd be giving you gold for this one. Thanks :) Commented Apr 7, 2019 at 13:51
  • 5
    Works nicely. It should be Cmd+click on "console" though, but that's a detail. Thanks!
    – Loolooii
    Commented May 16, 2019 at 14:28
  • If having trouble with F12 or Cmd+click, right click on "console" and then "go to definition" also works ( at least on osx )
    – imekinox
    Commented Jun 11, 2019 at 14:47
  • 7
    This is editing a file inside the node_modules dir meaning the 'fix' will be reset once you do a npm install again. I get it's a quick fix but no where near acceptable Commented Jun 21, 2019 at 20:23
  • To all those who don't want to modify a declaration file, the TS/JS Nightly Plugin fixes this for VSCode 1.36+
    – Andrew Li
    Commented Jul 18, 2019 at 16:01
36

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.

9
  • 11
    yeah that also causes no other auto import work anymore. I like this feature and do not accept this as an answer...:-D (thanks though!) There must be another solution. I am still looking as well.
    – dragonsoul
    Commented Nov 22, 2018 at 8:49
  • I found out that it does not happen when you create a new project with create-react-app my-app --scripts-version=react-scripts-ts so it must be connected to the workspace settings somehow.
    – dragonsoul
    Commented Nov 22, 2018 at 9:29
  • I did some more research, see the edited answer, hope you find it in your project!
    – KvD
    Commented Nov 23, 2018 at 15:29
  • 1
    Yes That worked indeed. I am thinking, is this a VSCode bug or from the creators of these packages? For me it was in @types/node and @types/react-dom. Thank you :-)
    – dragonsoul
    Commented Nov 26, 2018 at 21:12
  • 4
    found the faulty code inside "typescript/3.2/node_modules/@types/node/index.d.ts" which is inside ~/Library/Caches/ folder. I am on a mac. Since it's in my caching workspace I just removed the actual code: ``` lang-js declare module "console" { export = console; } ``` And it worked!
    – jcperez-ch
    Commented Jan 10, 2019 at 16:44
19

The most elegant solution that I found is to create dummy console.d.ts file somewhere in your project:

declare module 'console' {
    export = typeof import("console");
}

This will prevent auto-importing.

Credits: https://github.com/Microsoft/TypeScript/issues/30471#issuecomment-474963436

3
  • This is an elegant solution that can be checked in and will work across multiple workspaces and devices. Thanks!
    – phsource
    Commented Jun 27, 2019 at 23:22
  • 5
    Why do I get “The expression of an export assignment must be an identifier or qualified name in an ambient context.ts(2714)”? Might be related to some setting in my tsconfig.json? Commented Jul 10, 2019 at 20:11
  • Yes it could... You can check my settings here: github.com/codechecks/monorepo/blob/master/tsconfig.json It works with console.d.ts file Commented Jul 11, 2019 at 7:20
18

If you add a snippet for inserting console.log and use that instead, there will be no auto-import of "console"

https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets

here is my snippet:

{
    "Print to console": {
        "prefix": "cl",
        "body": [
            "console.log('$1');",
        ],
        "description": "Log output to console"
    }
}
4
  • 6
    Although is not a solution, it's clever and it works when you can't delete the package which is causing the problem, as stated in most voted answer.
    – tibuurcio
    Commented Jan 29, 2019 at 13:34
  • I agree with @tibuurcio . Clever workaround, but I cannot accept this as a solution...
    – dragonsoul
    Commented Feb 1, 2019 at 7:07
  • didn't work. it still auto imports after i write console then i press "." it auto imports Commented Feb 14, 2019 at 13:29
  • 1
    @mr-ghamkhar if you want to use the above snippet you just need to write "cl" or whatever you put in "prefix". It needs to be something else than "console" to be able to avoid the autoimport behavior.
    – Oskar
    Commented Feb 16, 2019 at 2:02
10

If you like me forgets "cl", you can use multiple prefixes in snippets:)

{
    "Print to console": {
        "prefix": ["cl","co","con","cons","conso","consol","console", "console.l","console.lo","console.log"],
        "body": [
            "console.log($1);",
        ],
        "description": "Log output to console"
    }   
}
2
  • 1
    The true answer is lazy :)
    – FOLOF
    Commented Mar 12, 2019 at 18:26
  • This is the best solution for me, because you don't need to edit internal packages
    – Harrison
    Commented Mar 29, 2019 at 19:07
6

One solution could be "editor.snippetSuggestions": "top", in settings. And to have console.log snipped defined, obviously.

2
  • This was the answer I was after! Thanks
    – Shmuli
    Commented May 28 at 20:09
  • very useful thank you!
    – mikrec
    Commented Jun 18 at 15:53
5

One way to prevent this from happening is to modify your tsconfig.json file to limit the set of types that are automatically imported into your project.

I had this same problem, and I fixed it by adding:

types: []

into my tsconfig.json file. What this does is disable's TypeScript (and by extension VSCode) from automatically importing all node packages that being with @types/ into the project configuration. This doesn't prevent TS from importing those type definitions if you explicitly import a package using those types.

In my specific case, the console definition was coming from @types/node, which had been imported into the project as a dependency of Storybook. However, my project was a webpack project, intended to run in a browser, so importing Node.js types into my source code made no sense. The base set of types that you would want to use in a browser are dom types, not node types.

Depending on your project, you may have to explicitly add the set of base type packages into the types parameter (types: ["dom", "react"] and so on). However, in my case this turned out to be unnecessary, my project was able to compile just fine with an empty list. And the tendency of VSCode to automatically import 'console' appears to have completely gone away; I haven't noticed any other ill effects so far.

More information on setting types in tsconfig.json here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

0

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