Follow these steps:
- Add
"type": "module"to your package.json. - Replace
"main": "index.js"with"exports": "./index.js"in your package.json. - Update the
"engines"field in package.json to Node.js 12:"node": "^12.20.0 || ^14.13.1 || >=16.0.0". - Add
"module": "ES2020"to your tsconfig.json. - Use only full relative file paths for imports:
import x from '.';→import x from './index.js';. - Remove
namespaceusage and useexportinstead. - Optional but recommended, use the
node:protocol for imports. - You must use a
.jsextension in relative imports even though you’re importing.tsfiles.
if you use ts-node
use it like:
node --loader ts-node/esm ./entrypoint.ts
Edit:
source gist