question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: support Node `require` in ES Modules

See original GitHub issue

Search 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.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5f59f3dc7a0965fb1767cf13c670c633bbbbf0a8/types/node/globals.d.ts#L190-L193

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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewbranchcommented, Sep 7, 2021

Note that in #44501, the TS import Foo = require("...") will be desugared to a createRequire’d require call.

0reactions
Lemminghcommented, Sep 4, 2021

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:

import { createRequire } from "module";
const require = createRequire(import.meta.url);

Then, I guess changing @types/node type definitions won’t help, but TypeScript needs to resolve semantics for module.createRequire().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found