Skip to content

Bug Typescript Nigtly .js moduleResolve nodenext - usage of require not detected properlyΒ #48716

Closed
@frank-dspeed

Description

@frank-dspeed

Bug Report

πŸ”Ž Search Terms

require not used

πŸ•— Version & Regression Information

  • I was unable to test this on prior versions because not needed

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import module from 'module';
/**
 * Wrapper that exposes the CJS version of the bindings
 * to get imported in ESM 
 * @deprecated use import('node-cblite/cblite.js)
 */
const require = module.createRequire(import.meta.url);
const cblite = require('./binding.cjs');
export { cblite };

πŸ™ 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

		"module": "ESNext",
		"target": "ESNext",
		"moduleResolution": "nodenext",

and yes there are 2 package.json near that one in the project dir and one even in the outer ../

both similar whole content

{ "type": "module" }

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions