WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Auto-imports insert import statement into CJS files #2247

@gebsh

Description

@gebsh

Extension Version

0.20251205.1

VS Code Version

1.106.3

Operating system Version

macOS Tahoe 26.1

Steps to reproduce

  1. Clone https://github.com/gebsh/auto-import-cjs.
  2. Open the cloned repository in VS Code.
  3. Open the file main1.js or main2.js, type LIB and choose the LIB_VERSION import from ./lib from the import suggestions.

Issue

In a JavaScript project that uses the .js extension for all files and has type: "commonjs" set in package.json, TypeScript auto-imports items via the import statement when module is set to node16, node18, or node20. For example, using this setup:

// package.json
{
	"private": true,
	"type": "commonjs"
}
// tsconfig.json
{
	"compilerOptions": {
		"allowJs": true,
		"module": "node20",
		"checkJs": true,
		"noEmit": true
	}
}
// lib.js
module.exports = { LIB_VERSION: 1 };
// main.js
module.exports.foo = 0;

When I try to auto-import LIB_VERSION in main.js, it's inserted as:

import { LIB_VERSION } from "./lib";

which is invalid syntax for a CJS file. I'd expect it to be imported via require(), since the module type of this file can be inferred from package.json.

Explicitly setting moduleDetection to auto fixes this but only if the file where I'm auto-importing an item already has CJS syntax. In empty files, the auto-import still uses the import syntax. Surprisingly, changing the file extensions to .cjs makes it even worse, because then the moduleDetection workaround doesn't work at all. It's possible to delete the module setting altogether, which fixes the problem in both empty files and files with CJS syntax, but AFAIU the TypeScript's module settings, I'm supposed to use nodeXX as that code is run in Node.

This happens in both TypeScript 5.9 and TypeScript Native. I'm reporting this here as per microsoft/TypeScript#62827.

I did a bit of debugging in TS 5.9, and AFAICT this is caused because node20 implicitly sets moduleDetection to force, which in turn switches useRequire in the auto-import code to false. That implicit switch to force is surprising, considering the documentation of moduleDetection:

There are three choices:

  • "auto" (default) - TypeScript will not only look for import and export statements, but it will also check whether the "type" field in a package.json is set to "module" when running with module: nodenext or node16

but I assume the documentation might be outdated/incorrect as it also lacks node18 and node20.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: EditorRelated to the LSP server, editor experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions