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

Named exports of CommonJS modules parse incorrectly if enumerable is !0 #45016

Closed
RomanHotsiy opened this issue Oct 15, 2022 · 1 comment
Closed

Comments

@RomanHotsiy
Copy link

Version

v18.11.0

Platform

Darwin 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

3 files:

bin.mjs

import { test } from 'a';

console.log(test);

node_modules/a/package.json

{
  "name": "main",
  "version": "1.0.0",
  "main": "index.js"
}

node_modules/a/index.js

'use strict'
Object.defineProperty(exports, '__esModule', { value: true });
exports.test = void 0;
var test = { value: 123 };
Object.defineProperty(exports, 'test', { enumerable: !0, get: function () {
  return test.value;
}});

Run:

node index.mjs

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

This script should work and log 123.

What do you see instead?

import { test } from 'a';
         ^^^^
SyntaxError: Named export 'test' not found. The requested module 'a' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'a';
const { test } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Additional information

If I change enumerable: !0 to enumerable: false everything works fine.

Most likely caused by nodejs/cjs-module-lexer#64

@aduh95
Copy link
Contributor

aduh95 commented Oct 15, 2022

Most likely caused by nodejs/cjs-module-lexer#64

Most likely, that looks like a duplicate even. The issue would need to be fixed on the other repository.

@aduh95 aduh95 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants