Skip to content

Typescript ESM issue聽#48835

Description

Bug Report

馃攷 Search Terms

ESM build error

馃晽 Version & Regression Information

Its in the nightly @next build do to with building an ESM package.

馃捇 Code

Hey guys, i need help i have a package im trying to convert to esm i have updated the package.json to type: module and the stuff in tsconfig.

https://github.com/The-Code-Monkey/TechStack/tree/feature/tcm-cli-to-esm/packages/tcm-cli

This is the package location, I am confused as it builds but when i try and use the dist index.js to lint the package it throws an error saying

Object.defineProperty(exports, "__esModule", { value: true });
^

ReferenceError: exports is not defined in ES module scope

This is because that line is being added by typescript to the index.js file within dist, there arent any other uses of the word exports in the dist folder so im not sure why typescript is adding it.

馃檨 Actual behavior

It adds the Object.defineProperty(exports, "__esModule", { value: true }); line into each of my files

馃檪 Expected behavior

This line shouldn't be added as im targeting ES2020 and nodenext with type: module.

which means it should compile down to esm but tsc is still compiling down to cjs for some reason.

Activity

  1. andrewbranch commented on Apr 25, 2022

    @andrewbranch
    Member

    This is happening because moduleResolution is set to node. Normally moduleResolution would not effect the module emit, but module resolution is kind of tied up in deciding whether a given file is a CJS or ESM module. Wesley Wigham (@weswigham) is this expected? I would have thought we would get the impliedNodeFormat just with module.

  2. andrewbranch commented on Apr 25, 2022

    @andrewbranch
    Member

    (That said, Andy Wilson (@The-Code-Monkey), you should unset moduleResolution so it defaults to the right thing.)

  3. andrewbranch commented on May 2, 2022

    @andrewbranch
    Member
  4. weswigham commented on May 24, 2022

    @weswigham
    Member

    I mean, technically it's working as intended - moduleResolution at node essentially forbids us from reading modern package metadata from package files/extensions, so that data isn't available for emit to base its' decisions on, which, in turn, means everything compiles as modern-node-cjs, which still might be what you want, since that's the only emit target that is commonjs, but doesn't transform dynamic import.

  5. lednhatkhanh commented on May 25, 2022

    @lednhatkhanh

    So I'm facing a problem when I set moduleResolution to node then I will face this problem, but setting it to node16 then I will face: #49160. So basically node16 is unusable for me.

  6. typescript-bot commented on May 27, 2022

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions