-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Bug Report
π Search Terms
cjs require
π Version & Regression Information
- This changed between versions
4.0.5and4.2.3
β― Playground Link
Playground link with relevant code
π» Code
// @filename: index.js
// @checkJs: true
/**
* @param {string} str
*/
function require(str) {}
const someModule = require("google-closure-compiler");
export {someModule};π Actual behavior
TS2792 occurs at the call to require():
const someModule = require("google-closure-compiler");
// ^^^^^^^^^^^^^^^^^^^^^^^^^----- Cannot find module 'google-closure-compiler'. option?
// Did you mean to set the 'moduleResolution' option to 'node',
// or to add aliases to the 'paths'What's worse, if the project contains types in node_modules/@types, these will now be included in the project, regardless of the value set for "typeRoots": [] in tsconfig.json, which is supposed to prevent this sort of behaviour.
From https://www.typescriptlang.org/tsconfig#typeRoots:
If
typeRootsis specified, only packages undertypeRootswill be included.
For me this caused a long and painful process of figuring out why I kept getting errors for imports missing from the "path" module, even though I definitely disabled node imports.
Here you can download a reduced test case of the situation described above.
Run tsc --noEmit -p jsconfig.json in the folder and you'll get an error stating that fromFileUrl is not in the "path" module, even though node_modules/@types has been disabled and a custom path has been added in jsconfig.json.
π Expected behavior
Either no error, or an error informing me that I can't use require() as it's already taken by the compiler. The latter is already the case with TypeScript files:
function require(str: string) {}
// ^^^^^^^------ TS2441: Duplicate identifier 'require'.
// Compiler reserves name 'require' in top level scope of a module.It's just missing inside JavaScript files.
π Workarounds
Just don't name your functions require π
. But you'd have to know that this issue is happening in the first place, which is not easy to figure out with no errors telling you this.