vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.27k stars 291 forks source link

ESM with typescript: imported module is undefined #961

Open jacek-jablonski opened 2 years ago

jacek-jablonski commented 2 years ago

Hi, I've got a very simple repository reproducing my problem: https://github.com/jacek-jablonski/example-ncc/

In my code, I import GitHub library and try to use it:

import github from "@actions/github";

//...
const octokit = github.getOctokit(token);

After compiling above with ncc above code produces error:

TypeError: Cannot read properties of undefined (reading 'getOctokit')
    at searchReposByTopic (file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10447:40)
    at run (file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10421:63)
    at runWrapper (file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10425:15)
    at Object.8889 (file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10431:10)
    at __nccwpck_require__ (file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10591:44)
    at file:///Users/jacek/Projects/Personal/example-ncc/actions/lib/search-repos-by-topic/index.js:10616:36
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)

To fix this problem, I have to import specific function like this:

import { getOctokit } from "@actions/github";

//...
const octokit = getOctokit(token);

However, I am still wondering why the first version doesn't work as it should? Do you have any idea why? Thanks.

styfle commented 2 years ago

If its ESM, its likely because there is no default export defined in that module.

If you want to import all exports from the module, you can use a wildcard import * as github from "@actions/github".

Although importing the specific export you want to use is best.

jacek-jablonski commented 2 years ago

Thanks for explanation @styfle. The only thing that is strange about all of this is that no compile-time errors are reported.