-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Bug Typescript Nigtly .js moduleResolve nodenext - usage of require not detected properly #48716
Comments
I canβt reproduce this. Please provide a full repro. |
Ah, the key piece of info is that this is a JS file. In JS we seem to assume any invocation of |
It seems likely this has the same root cause / is basically the same bug as #48726 |
I guess your correct i code a lot of low level stuff so i code it in CJS as you know maybe there is no access to native code directly inside ESM. All the import magic is the work of the cjs-module-lexer that trys to create namedExports for the native code and CJS modules. The engine it self is always CJS no matter if you even do node my.mjs node stays CJS as the C Code that creates the Environment is Sync by default for good reason. when we do require('module') we get directly the memory pointer to the module C code and the createRequire can be used many times with diffrent path as you sayed It is localy scoped shadowing the global. but i also spotted many diffrent small issues while i am coding with real scopes
that is the code that really is written into any .CJS file .cjs files are not handled like .js files nodejs did also restrict for example: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode thats why .CJS default to strict mode but it is documented no where. In general the module lexer will choose if ti is JS CJS or ESM so while there are only 2 module systems there are in reality 3 as useStrict is also a Module System that is able to register globals via sideEffects. the old JQuery Module system i call it that way because thats where all did use it and saw it in action most of the time. Extra BonusThe Typescript Checker aka Program out of history needs to create 3 isolated scopes 2 follow sync processing rules and 1 following async only including order resolution. in my universal Module System i have a lot of filds that i need to neogate module order.
after that general file loading neogation comes the scope evaluation there we read all the code but abstracted away the general Module Systems like CJS and ESM now it is only JS and we need to look how we run that JS in its correct Scope so that we know where the results are like globalThis assignment or if var got used in a file loaded before .cjs and so on the whole code ast needs to get interpreted into its runtime version. shorti wanted to point out that maybe the whole scope system needs a rework for example use stric also removes octal literals and so on while ES6 introduces to write them with the the octal and require values all this is out of my view type related and the resulting type is scope related but typescript is not aware about real global scope and its code state at a given time so it can not infer the correct type or produce the correct type without getting hardcoded at present. |
the playground link also reproduces the error but the playground link is not able to preserve the settings you need to adjust source javascript ESNext ESNext and then you get the error the playground is broken: |
Like #48726, I think that the common case is best supported by the current assumption that |
Bug Report
π Search Terms
require not used
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
src/native/binding.js:7:7 - error TS6133: 'require' is declared but its value is never read.
7 const require = module.createRequire(import.meta.url);
π Expected behavior
require is used it can be fixed via renaming it to Require with CamelCase
i guess this is part of the nodenext resolve and that js does not preserve the require call and will translate it even when it does not come from the nativ require so require is some how now a reserved word in .js files which is not expected! as they are modules defined by package.json type module
this is even more evil when you target other environments that also got a own require implementation no matter which one require-js or the graaljs engine the deno engine anything running on raw v8 that implements require,
require is a relativ common function name in many environments and does not match the NodeJS.Require type
Solution
When moduleResolution: nodenext + target: ESNext that also implicit sets preserve require in all current nodejs versions and environments require should stay require because it only exists for backward compat and is most time always a shim as we produce nodeNext from ESNext for ESNext
and yes there are 2 package.json near that one in the project dir and one even in the outer ../
both similar whole content
The text was updated successfully, but these errors were encountered: