LoginSignup
3
2

More than 5 years have passed since last update.

firebase functionsをwebpackでビルドデプロイする

Posted at

何も考えずにwebpackでビルドしてそのままfirebase functionsにデプロイしようとしたらエラーになったのでメモ

問題

firebase functionsにデプロイする際はモジュールをnodeランタイムとしてビルドする必要がある
webpackで何も指定せずにビルドするとブラウザ用にビルドされる
今回自分が作ったモジュールにはPhaserが入っていたのでデプロイ時にエラーった

Module not found: Error: Can't resolve 'fs' in

よくない対応

以下のようにwebpackに追記するとエラー回避はでき、ブラウザ向けビルドは完了するが
モジュールが正常に動くかは保障できない

webpack.conf.js
node: {
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty',
  },

よい対応

nodeランタイム用にinstall

npm install --save-dev webpack-node-externals
webpack.conf.js
// 追記
var nodeExternals = require('webpack-node-externals');

module.exports = {
    entry: './src/index.ts',
    output: {
        filename: 'index.js', 
        libraryTarget: 'this' // 追記
    },
    target: 'node', // 追記
    module: {
        // 割愛
    },
    resolve: {
        extensions: [ '.tsx', '.js' ]
    },
    externals: [nodeExternals()] // 追記
};
3
2
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
3
2