Suggestion: support Node `require` in ES Modules
See original GitHub issueSearch Terms
node require commonjs webpack conditional dynamic import
Suggestion
Node style require
’s are often used inside ES Modules for conditional imports. For example:
const getSentry = () => {
if (__CLIENT__) {
return require('./client-sentry').default;
} else {
return require('./server-sentry').default;
}
};
export default getSentry();
In this example, require
returns any
as per the typings provide by @types/node
.
In order to acquire types, we have to use import
types:
type ClientSentry = typeof import('./client-sentry');
type ServerSentry = typeof import('./server-sentry');
const getSentry = () => {
if (__CLIENT__) {
return (require('./client-sentry') as ClientSentry).default;
} else {
return (require('./server-sentry') as ServerSentry).default;
}
};
export default getSentry();
I would like to suggest that TypeScript supports require
calls inside of ES Modules (in Node envs). This way the workaround would not be necessary. Alternatively, perhaps TypeScript could make it possible for @types/node
to improve the return type of require
, e.g.
interface NodeRequireFunction {
<Id extends string>(id: Id): typeof import(Id);
}
Note that require
is desirable over dynamic imports because it is synchronous.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Using ES modules in Node.js - LogRocket Blog
Adding support for ES modules in Node today Currently, this is no longer required as from version 13.14. 0 to 14.0.
Read more >require() of ES modules is not supported when importing node ...
node-fetch is an ESM-only module - you are not able to import it with require. We recommend you stay on v2 which is...
Read more >How to Use ECMAScript Modules in Node.js
Starting version 13.2.0, Node.js has stable support of ES modules. In this post, you'll learn how to enable and use ES modules in...
Read more >CommonJS vs ES Modules in Node.js - A Detailed Comparison
CommonJS modules are AMD-compliant and can be used in both Node.js and browsers. ES modules are not AMD-compliant but are supported in browsers ......
Read more >Avoid these issues when using new ECMAScript modules in ...
Node.js support for ECMAScript es6 modules ... The support for ECMAScript modules is stable as of Node.js 14. So there are no issues...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that in #44501, the TS
import Foo = require("...")
will be desugared to acreateRequire
’d require call.The example above seems not complete.
require()
does not exist in ES module on Node.js since draft. It needs to be constructed with helpers:Then, I guess changing
@types/node
type definitions won’t help, but TypeScript needs to resolve semantics formodule.createRequire()
.