Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.log auto imports import console=require('console'); #30471

Closed
mschipperheyn opened this issue Mar 10, 2019 · 25 comments

Comments

Projects
None yet
@mschipperheyn
Copy link

commented Mar 10, 2019

  • VSCode Version:1.31.1
  • OS Version:Mac OS Current

Steps to Reproduce:
type console.. 7 times out of 10, vscode will insert import console=require('console');in the imports top of the file leading to a compilation error.

Single most annoying issue of vs code by far.

Does this issue occur when all extensions are disabled?: Not sure

@mjbvz mjbvz assigned mjbvz and unassigned weinand Mar 10, 2019

@Rubenvdveen

This comment has been minimized.

Copy link

commented Mar 11, 2019

Same here (Ubuntu 18.04). It came with the new VSCode update.. Very annoying.

@Zlass

This comment has been minimized.

Copy link

commented Mar 18, 2019

Seems to be caused by node's type definition. Removing:

declare module "console" {
    export = console;
}

in ../node_modules/@types/node/index.d.ts seems to be a temporary fix until that definition gets updated. Or it could be caused by another installed node module that declares a type definition for console. You can figure it out by following the path of where ever import console=require('console') leads to. Credit to this stackoverflow.

@mjbvz mjbvz transferred this issue from microsoft/vscode Mar 18, 2019

@mjbvz

This comment has been minimized.

Copy link
Contributor

commented Mar 18, 2019

Does this reproduce in the latest VS Code insiders build with all extensions disabled? Make sure you are using the latest TypeScript version too

Also @Zlass which version of the node typings are you using? I don't see that declaration in the latest @types/node and have not been able to repro this issue

@Zlass

This comment has been minimized.

Copy link

commented Mar 18, 2019

Ok, I believe I found the cause. Seems like it's a bug related @types/styled-components having a dependacy on @types/react-native, which conflicts with the node's console type.

I was able to reproduce it in the insider build by creating a new typescript react app with create-react-app and installing @types/styled-components.

I don't think this is a VS Code issue and there's already an open issue for it in DefinitelyTyped here

@mschipperheyn

This comment has been minimized.

Copy link
Author

commented Mar 19, 2019

Hmmm, I don't use styled components. Not impossible that it came along with some other library

@Zlass

This comment has been minimized.

Copy link

commented Mar 19, 2019

@mschipperheyn I'd recommend checking what shows up when you try to auto-complete console. I was able to figure out what package was declaring another definition of console by peeking at the references of each auto-complete suggestion.

@SukhKalsi

This comment has been minimized.

Copy link

commented Mar 20, 2019

I'm experiencing the same (on version 1.30.2). It's a React Native project, not using Styled Components (but any other lib might). When typing console below is the respective output of auto complete. The first does an auto import, the second does an auto require.

Screen Shot 2019-03-20 at 17 47 43

Edit: Seems like on my setup its pointing to the following ~/Library/Caches/typescript/3.2/node_modules/@types/node/index.d.ts on line 207

@BrendonSled

This comment has been minimized.

Copy link

commented Mar 20, 2019

Add a console.d.ts file to your src root and add the following contents:

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

That will fix your import

@SukhKalsi

This comment has been minimized.

Copy link

commented Mar 20, 2019

Thanks @BrendonSled for that temp fix... that seems to have resolved it for me - now to add this to all my other projects 🙈

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Mar 23, 2019

This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@03t02

This comment has been minimized.

Copy link

commented Mar 29, 2019

@BrendonSled thanks you !! Finally a tmp solution

@DanFoad

This comment has been minimized.

Copy link

commented Apr 2, 2019

Anyone have an update on this beyond temporary solutions? Google only returns this link and the StackOverflow link posted before

@aloehh

This comment has been minimized.

Copy link

commented Apr 4, 2019

@BrendonSled thx for your solution

@jamespacileo

This comment has been minimized.

Copy link

commented Apr 28, 2019

This is driving me crazy...

@atrauzzi

This comment has been minimized.

Copy link

commented Apr 30, 2019

Argh, this is so annoying!

@mattcarlotta

This comment has been minimized.

Copy link

commented May 2, 2019

Same problem here. I'm on Linux Mint 19.1.

The only way I can get around this auto import feature is to either:

Go to File ▶️ Preferences ▶️ Settings ▶️ Search for Javascript Auto Imports and/or Typescript Auto Imports ▶️ uncheck Enable/disable auto imports suggestions.

OR

Go to File ▶️ Preferences ▶️ User Snippets▶️ Select global.code-snippets ▶️ Save it to /vscode/snippets/global.code-snippets ▶️ then add and save this global snippet (alternatively, you can create a langauge specific snippet if you don't want it to be global -- this will make the console snippet below the first entry in the auto suggestions list):

{
  "Print to console": {
    prefix: ["console", "console.", "console.l", "console.lo", "console.log"],
    body: ["console.log($1);"],
    description: "Log output to console"
  }
};
@EKoetsjarjan

This comment has been minimized.

Copy link

commented May 6, 2019

same problem here visual studio 1.33.1 node 10
temp solution worked for me...

@funwithtriangles

This comment has been minimized.

Copy link

commented May 9, 2019

This is infuriating. Would be great if we could blacklist certain auto imports.

@AndrewMorsillo

This comment has been minimized.

Copy link

commented May 31, 2019

This is exceedingly annoying. The workaround posted by @BrendonSled is invalid according to strict ts. Any other way to fix this?

'export=' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.   ts(7022)
@lettertwo

This comment has been minimized.

Copy link

commented Jun 3, 2019

@AndrewMorsillo give some suggestions on this issue a try.

In particular, both this suggestion and this suggestion seem to have improved the situation for me.

@mjbvz

This comment has been minimized.

Copy link
Contributor

commented Jun 19, 2019

@RyanCavanaugh Any way we can improve the experience even if there is an incorrect d.ts from a package?

@coppyC coppyC referenced this issue Jun 25, 2019

Closed

Delete console.d.ts #36431

3 of 16 tasks complete
@timothystewart6

This comment has been minimized.

Copy link

commented Jul 15, 2019

Credit to @mattcarlotta for his answer. I am just formatting this for JSON since VSCode doesn't seem to respect it otherwise:

{
	"Print to console": {
	  "prefix": ["console", "console.", "console.l", "console.lo", "console.log"],
	  "body": ["console.log($1);"],
	  "description": "Log output to console"
	}
}

This has been driving me 🙃 on React Native projects

@DanielRosenwasser

This comment has been minimized.

Copy link
Member

commented Jul 16, 2019

This issue should have been fixed. If you're still running into this, please

  1. ensure that you still see the behavior after installing this plugin
  2. provide a minimal reproduction of the issue.
@coppyC

This comment has been minimized.

Copy link

commented Jul 16, 2019

@DanielRosenwasser
I install the plugin and then lost any auto import.
I want the auto import, but dont't want to auto import the console.

@DanielRosenwasser

This comment has been minimized.

Copy link
Member

commented Jul 16, 2019

Okay, that sounds like a separate issue that you should file on https://github.com/Microsoft/TypeScript if you can provide a minimal reproduction of that problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.