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

Typescript ESM issue #48835

Closed
The-Code-Monkey opened this issue Apr 25, 2022 · 6 comments
Closed

Typescript ESM issue #48835

The-Code-Monkey opened this issue Apr 25, 2022 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@The-Code-Monkey
Copy link

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.

@andrewbranch
Copy link
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. @weswigham is this expected? I would have thought we would get the impliedNodeFormat just with module.

@andrewbranch
Copy link
Member

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

@andrewbranch
Copy link
Member

@weswigham is this a bug?

@weswigham
Copy link
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.

@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 24, 2022
@lednhatkhanh
Copy link

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.

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants