- ./node_modules/ の直下にパッケージ名ディレクトリを作る。e.g. ./node_modules/my-hello-pkg/
- パッケージ名ディレクトリの直下に package.json を書く。
- パッケージ名ディレクトリの直下に index.js と index.d.ts を置く。tsc --declaration index.ts で書き出してくれる。
- オプショナルに、パッケージ名ディレクトリの直下に readme.md を置く。
package.json:
{ "name": "my-hello-pkg", "version": "1.0.0", "description": "Hello, world.", "type": "module", "main": "index.js", "types": "index.d.ts", "files": [ "index.d.ts", "index.js" ] }
index.js:
export function hello() { console.log("Hello, world.") }
index.d.ts:
export function hello(): void;
使う側(拡張子は .mjs):
// my-hell-test.mjs import {hello} from 'my-hello-pkg'; hello();
実行:
node ./my-hello-test.mjs