Skip to content
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

esm: add pjson.importInterop to support __esModule #40902

Closed
wants to merge 1 commit into from

Commits on Nov 21, 2021

  1. esm: add pjson.importInterop to support __esModule

    Adds an option "importInterop" to package.json. When enabled, `import`
    statements in that package will behave slightly different with respect
    to default exports if the imported module is CJS.
    
    - When the imported module defines `module.exports.__esModule` as a
      truthy value, the value of `module.exports.default` is used as the
      default export.
    - Otherwise, `module.exports` is used as the default export.
      (existing behavior)
    
    It allows better interoperation between full ES modules and CJS modules
    transformed from ES modules by Babel or tsc. Consider the following
    example:
    
    ```javascript
    // Transformed from:
    // export default "Hello";
    Object.defineProperty(module.exports, "__esModule", { value: true });
    module.exports.default = "Hello";
    ```
    
    When imported from the following module:
    
    ```javascript
    import greeting from "./hello.cjs";
    console.log(greeting);
    ```
    
    with the following package.json:
    
    ```javascript
    {
      "type": "module",
      "importInterop": true
    }
    ```
    
    Then it will print "Hello".
    
    Fixes: nodejs#40891
    qnighy committed Nov 21, 2021
    Configuration menu
    Copy the full SHA
    6bf95a8 View commit details
    Browse the repository at this point in the history