lukeed / tsm

TypeScript Module Loader
MIT License
1.18k stars 19 forks source link

Default exports suggested when run via `tsm` #41

Closed faustbrian closed 1 year ago

faustbrian commented 2 years ago

So I'm currently playing around with tsm to get a better grasp of how it works and to create a smaller test runner alike https://github.com/lukeed/uvu but seem to run into an odd issue that the uvu runner doesn't encounter. Whenever I attempt to import files while running my script via tsm ./bin/runner.mjs I run into errors that suggest that I should use default imports.

Runner

export async function runner(suites: string[]) {
    for (const suite of suites) {
        await import(suite)
    }
}

Error

import { format } from 'string-kit';
         ^^^^^^
SyntaxError: Named export 'format' not found. The requested module 'string-kit' 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 'string-kit';
const { format } = 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:528:24)

Now uvu doesn't run into this issue but I assumed tsm would make sure the .ts files get properly handled. Any pointers at what could cause this are appreciated!

Edit 1: Directly executing via tsm source/hash.test.ts also seems to fail so I assume this isn't related to doing an import via code but a general issue. Edit 2: I guess it's caused by allowSyntheticDefaultImports which uvu seems to handle fine? Does this need special handling outside of tsm?

lukeed commented 1 year ago

this isn't to do with tsm; good luck with your library!