Skip to content

Compile as ESM from TS using ncc #2394

Answered by RobinTail
nkooman-bzs asked this question in Q&A
Discussion options

You must be logged in to vote

Then the message you get is caused by the combination of three reasons, @nkooman-bzs :

  • __filename is the CJS shorthand which is not present in ESM
  • you are using ncc that bundles everything, including typescript, that contains __filename, and ncc does not transform it accordingly
  • the result bundle is treated as ESM because the nearest package.json has type: "module" entry
    • it says that it's located in ..\dist\package.json, so check it out

My advice:

  • consider tsup for transforming your Typescript code, or tsx for running your Typescript code;
  • or explore ncc documentation on how either:
    • to externalize typescript
      • -e, --external [mod] Skip bundling 'mod'. Can be used many times

    • or…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@nkooman-bzs
Comment options

@RobinTail
Comment options

Answer selected by RobinTail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants