Closed faustbrian closed 2 years ago
You can use a config file to force/configure behavior for a specific extension. So, if you want all .js
files to be loaded as ESM, you can do this:
// tsm.js
import { define } from 'tsm/config';
export default define({
'.js': {
format: 'esm'
}
});
However, if you're doing something like uvu -r tsm
or node --require tsm
then you'll still run into issues. This is because you're using a require hook to load a file. That file cannot contain ESM since it was required. It'd be like trying to do require('./file.mjs')
If you invoke tsm
directly as a CLI (or via node --loader tsm
) then that Node process will be expecting/able to handle ESM. It will also be able to handle any CommonJS module exports.
Hopefully that helps! I believe the docs touch on this too
Hey, I'm using a build setup like https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html to support both ESM and CJS but that seems to cause an issue with https://github.com/lukeed/uvu which seems to come from tsm when looking at the stack trace.
The TLDR of the above article is that instead of your package.json having a type property you defer it into the distribution folder. So you have
dist/cjs/package.json
anddist/esm/package.json
files which indicate how to treat the loading. I assume that tsm decides if it should use require or import based on the type property in the package.json file.Is there any way to force tsm to run as if the package.json file had a type property that is set to module?