LoginSignup
6
6

More than 5 years have passed since last update.

browserifyで動的生成するパスのrequireをしたい

Last updated at Posted at 2015-03-08

browserifyは「事前にモジュールを探して結合する」という仕様上、requireのパスが動的に変わる場合にその解決ができません。

例えば、以下のコードはコンパイルは通りますが、ブラウザ上で実行した際に "Cannot find module './path/to/foo'" というエラーになります。

var fooPath = './path/to/foo';
require(fooPath);

なんで、これを読み込めるようにしたい場合は、対象のモジュールを

  • コマンドラインからコンパイルする場合は --require を使うか
  • JSからコンパイルする場合は require を使うか

またはこうする

// 実行する必要はない
if (false) {
  require('./path/to/foo');
}

var fooPath = './path/to/foo';
require(fooPath);

参考

6
6
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
6
6