-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
When using require()
in es modules, no TS2441 error occurs and node_modules/@types
is automatically included in the project.
#48726
Comments
π Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript. Issue body code block by @jespertheend β Failed: -
Historical Information
|
@typescript-bot bisect good v4.0.5 bad v4.1.5 |
The change between v4.0.5 and v4.1.5 occurred at c3d41bb. |
After the discussion on #50130, I think the right fix is to show an error; However, the current error is emit-oriented; it'll show up in JS or TS files if your tsconfig targets es5, but that is not common, for the JS projects I've written at least. The error needs to show up for any JS file instead. |
It's been a while since I opened this, so the exact details are a bit fuzzy. But fwiw my use case was that I'm using deno for running and import { createRequire } from "https://deno.land/[email protected]/node/module.ts";
const require = createRequire(import.meta.url);
const closureCompiler = require("google-closure-compiler"); I worked around the issue by renaming An error at |
Interesting. It looks like the ideal would be for typescript to be able to resolve |
I reckon my setup is highly unusual xD. I don't think anyone else uses |
I think after all that I'm inclined to say that the current behaviour is good for most cases, and that the usual desired outcome would be for |
One of the things I softly pitched in #50152 was a module resolution mode in which |
Bug Report
π Search Terms
cjs require
π Version & Regression Information
4.0.5
and4.2.3
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
TS2792 occurs at the call to
require()
: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": []
intsconfig.json
, which is supposed to prevent this sort of behaviour.From https://www.typescriptlang.org/tsconfig#typeRoots:
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 thatfromFileUrl
is not in the"path"
module, even thoughnode_modules/@types
has been disabled and a custom path has been added injsconfig.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: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.The text was updated successfully, but these errors were encountered: