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
namespace
usage and useexport
instead. - Optional but recommended, use the
node:
protocol for imports. - You must use a
.js
extension in relative imports even though you’re importing.ts
files.
if you use ts-node
use it like:
node --loader ts-node/esm ./entrypoint.ts
Edit:
source gist