Skip to content

VSCode mixes import and require syntaxes on auto-import #29038

Description

@naoey

In this particular instance I encountered the bug with moment.js.

  • VSCode Version: 1.30.0-insider, 1.29.1
  • OS Version: Windows 10 (1803), macOS 10.14.1

Steps to Reproduce:

  1. Begin typing usage of an un-imported module
  2. Press enter on auto-import intellisense suggestion

Does this issue occur when all extensions are disabled?: Yes

2018-12-01_12-15-51

Above recording is on fresh Insiders install with extensions disabled and being reproduced in a fresh project with just React and moment installed through yarn.

Activity

  1. mjbvz commented on Dec 15, 2018

    @mjbvz

    Tested with 3.3.0-dev.2018121. In a blank js file that is not part of any jsconfig project, moment is added as:

    import moment = require('moment');
    
    moment()
  2. removed their assignment
    on Dec 15, 2018
  3. weswigham commented on Dec 17, 2018

    @weswigham
    Member

    If esModuleInterop is on, we should probably prefer default imports over import =.

  4. DanielRosenwasser commented on Dec 18, 2018

    @DanielRosenwasser
    Member

    Is that the ideal behavior, or current behavior?

  5. weswigham commented on Dec 18, 2018

    @weswigham
    Member

    Ideal, I believe

  6. jpike88 commented on Jan 10, 2019

    @jpike88

    I have this issue too, but I don't have esModuleInterop on as it's not needed, I'm using es7 lib and esnext as target and module

  7. filipef101 commented on Jan 16, 2019

    @filipef101

    Any solution?

  8. jpike88 commented on May 3, 2019

    @jpike88

    This is starting to drive me insane. Why is this even breaking like this? If a file has import statements, shouldn't the default auto-import behaviour stick to that?

  9. 25 remaining items

  10. derwaldgeist commented on Aug 18, 2020

    @derwaldgeist

    This suddenly started to happen in my project without any modifications to my configuration. Instead of importing via import, I now get require statements. This is pretty annoying. How can I change this back? I am using ES6, not Typescript.

  11. andrewbranch commented on Aug 18, 2020

    @andrewbranch
    Member

    Tom Brückner (@derwaldgeist) you’d have to give us a lot more detail about exactly how to reproduce your problem.

  12. marianovitale commented on Sep 1, 2020

    @marianovitale

    Hi, this is happening to me with Enzyme. Instead of importing via import, I now get require statements.
    I have not changed anything in my project. Also, like Tom Brückner (@derwaldgeist) i am using ES6, not Typescript.
    I have the latest version of vscode. I also have the latest version of "JavaScript and TypeScript Nightly" installed.

    Any ideas?
    Should I give more information?
    example2

  13. andrewbranch commented on Sep 1, 2020

    @andrewbranch
    Member

    Luis Velasquez (@Marianovh) do you have a tsconfig.json or jsconfig.json file? Since this file doesn’t already have any imports or exports, and there’s (presumably) no config file that tells us that it can use ES6 imports, and the typings for enzyme didn’t indicate that the importing file specifically needed to use ES6 or CJS imports, we just had nothing to go on and defaulted to CJS. We could potentially look at more files in your program as a better heuristic. But an immediate fix would be to add a jsconfig.json file at the root of your repo with

    {
      "compilerOptions": {
        "module": "es2015"
      }
    }
  14. andrewbranch commented on Sep 1, 2020

    @andrewbranch
    Member

    Also, it should be noted that in any file that already has an ES6 import or export, it should do the right thing on future auto-imports in that file. If that’s not your experience, it’s a bug.

  15. marianovitale commented on Sep 2, 2020

    @marianovitale

    Andrew Branch (@andrewbranch) Thanks for your answer!
    Creating a jsconfig.json file with the configuration you passed worked perfectly!.
    Is there any reason why it take CJS as default? Can this be changed in an easy way?

    Also, it should be noted that in any file that already has an ES6 import or export, it should do the right thing on future auto-imports in that file. If that’s not your experience, it’s a bug.

    This is what happened, so it should not be a bug.

    Anyway, thank you very much for your help.

  16. andrewbranch commented on Sep 2, 2020

    @andrewbranch
    Member

    Not a great one. require just works in more places right now. The LTS node release requires experimental flags to work with ESM, and bundlers do their own module loading and most of them will work with CJS or ESM with a seamless interop layer. The default could definitely change in the future, if we see CJS get phased out of the Node world.

  17. derwaldgeist commented on Sep 2, 2020

    @derwaldgeist

    you’d have to give us a lot more detail about exactly how to reproduce your problem.

    To be honest, I don't have a clue what kind of information I could provide, but maybe this is just the wrong project to address this to (I came here via a Google search), since my problem is not directly related to TypeScript. I think, it's more a problem with the auto-import add-on itself. Sorry for bothering.

  18. andrewbranch commented on Sep 2, 2020

    @andrewbranch
    Member

    Tom Brückner (@derwaldgeist) There’s no auto-import add-on; JavaScript support in VS Code is implemented by TypeScript. But it matters a lot what you were importing, what imports (if any) the file already imported, whether you have a tsconfig/jsconfig file in your project, and if so, what settings it has. I’m guessing your scenario was similar to Luis Velasquez (@Marianovh)’s, where your file didn’t have any imports/exports/requires yet. I don’t think it’s very expensive for us to examine other files in your project to get a better idea for a default. That would have fixed Luis Velasquez (@Marianovh)’s issue without needing to add a config file. I’ll reopen this issue to explore a solution like that.

  19. derwaldgeist commented on Sep 3, 2020

    @derwaldgeist

    Andrew Branch (@andrewbranch) Thanks for the clarification. Well, at least for me, the biggest confusion was that everything worked perfectly - until all of a sudden auto-imports switched to require() statements instead of import statements. I never used any tsconfig / jsconfig files in my app, as I was not aware of them. I am using .eslintrc to configure things and (naively) assumed VSCode would use this config, too. Will try adding jsconfig now. I agree that parsing existing code is definitely not a good idea.

  20. nexsodev commented on Oct 24, 2020

    @nexsodev

    Same here. I not using Typescript nor have any config file, but after recent updates, auto import creates require statements instead ES6 import.
    My current version is:

    Version: 1.50.1 (system setup)
    Commit: d2e414d9e4239a252d1ab117bd7067f125afd80a
    Date: 2020-10-13T15:06:15.712Z
    Electron: 9.2.1
    Chrome: 83.0.4103.122
    Node.js: 12.14.1
    V8: 8.3.110.13-electron.0
    OS: Windows_NT x64 10.0.18363
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: LS: TSServerIssues related to the TSServerExperience EnhancementNoncontroversial enhancementsFix AvailableA PR has been opened for this issueSuggestionAn idea for TypeScriptVS Code PriorityCritical issues that VS Code needs fixed in the current TypeScript milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions