Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to ESM from CJS #1

Closed
peko-thunder opened this issue Feb 11, 2023 · 0 comments · Fixed by #2
Closed

to ESM from CJS #1

peko-thunder opened this issue Feb 11, 2023 · 0 comments · Fixed by #2

Comments

@peko-thunder
Copy link
Owner

peko-thunder commented Feb 11, 2023

commonjs => esm への移行

ソースコードの変更

https://zenn.dev/teppeis/articles/2021-10-typescript-45-esm

  • package.json
    • change => "type": "module"
  • tsconfig.json
    • change => "compilerOptions.module": "NodeNext"
    • remove => "compilerOptions.moduleResolution": "node"
  • ***.ts
    • import
      • change => .js

ここまでだとtsconfig.jsonに記載したpath解決をできなくなるため、実行時にパッケージを読み込むことができない

Cannot find package '@/modules' imported from C:\xxxxx

現状の解消方法としてはpath解決での読み込み自体をなくして、相対パスで各種ソースを読み込むものとする

"baseUrl": "./",
"paths": {
  "@/*": ["src/*"]
},

※ 例外としてts-node の実行時にloaderで個別ファイルを挟めば可能ではある
https://kimuson.dev/blog/typescript/ts_node_esm_paths/

開発ツールのesm対応

監視ツールとしてts-node-devを利用していたがesmに対応していないと思われるためnodemonに変更した
ts-node-devの方が動作が早いとのことで導入したがソースが小規模であることからnodemonでも問題ないだろうと判断

jestやeslintなどのconfig.jsファイルはconfig.cjsに変更し設定も変更する必要がある

peko-thunder added a commit that referenced this issue Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant