1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

parcelでライブラリをbuildするときにnode_modulesが含まれずError: Cannot find module...と表示される問題の解決法

Last updated at Posted at 2022-09-03

Parcelでライブラリをビルドする

最近のtypescript

だいたいビルドはparcelに任せてます。webpackの設定が面倒くさいので。まあ一度環境をつくってしまえばなんとかなるんですけど。

parcelでライブラリモードでbuildするとnode_modulesが含まれない

ので、tsファイルをビルドして、他のディレクトリに移動してからnodeで起動しようとするとmoduleが見つかりませんというエラーが出る。例としてrambdaを入れてビルドした場合。

Error: Cannot find module 'rambda'

ライブラリモードはnode_modulesが基本的には含まれないのだ

Determines whether to bundle node_modules or treat them as external. The default is true for browser targets, and false for library targets. Possible values are:

簡単に訳すとブラウザーターゲットのときはnode_modulesをバンドルしてくれるが、ライブラリモードのときはバンドルしてくれないらしい。ということで明示する必要がある。

 "targets": {
    "main": {
      "context": "node",
      "includeNodeModules": true,
      "optimize": true
    },
    "module": {
      "context": "node",
      "includeNodeModules": true,
      "optimize": true,
      "distDir": "./dist/module.js"
    }
  },

こんな感じでincludeNodeModulesをtrueにしておくとnode_modulesが含まれてくれる。

そういえばなぜかparcelがたまにpnpmだとうまくいかない

のでyarnにしてます。pnpmでparcelうまく動かんな〜という人はnode_modulesを消してyarnにしてみてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?