You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firebase云函数中引入Kotlin多平台生成的JS文件作为Node依赖

How to Use Kotlin Multiplatform's common-js.js as a Module Dependency in Cloud Functions

Hey there, let's first break down why your current approaches aren't working:

  • When you manually drop common-js.js into node_modules, Firebase's deployment process runs a fresh npm install on their servers. This overwrites or ignores manually added files, which is why your deployment fails.
  • Placing it outside node_modules breaks local runs because either your import path is incorrect, or the module format of the Kotlin-generated JS doesn't play nice with Cloud Functions' CommonJS environment.

Here's how to fix this properly:

1. Move common-js.js to a custom project directory

Create a folder like lib/ in your Cloud Functions root directory, then place common-js.js inside it. This keeps it separate from npm-managed dependencies and ensures Firebase will package it during deployment (as long as it's not listed in .gitignore).

2. Adjust imports for CommonJS compatibility

Kotlin Multiplatform often generates ES module-style code (with export statements), but Cloud Functions uses CommonJS. You have two solid options here:

Option A: Patch the Kotlin-generated file directly

Add this line at the end of common-js.js to expose its exports via CommonJS:

module.exports = {
  // List all the exports from the Kotlin file here. For example:
  // myUtilityFunction: myUtilityFunction,
  // appConfig: appConfig
};

Then import it in your function code using the correct relative path:

const commonJs = require('./lib/common-js.js');

Make sure the path matches where your function entry file (e.g., index.js) is located.

Option B: Use a local npm package (cleaner, scalable approach)

Turn your common-js.js into a local npm package to let npm handle module resolution automatically:

  1. Navigate to your lib/ folder and initialize a package:
    cd lib
    npm init -y
    
  2. Update the package.json in lib/ to set your JS file as the main entry:
    {
      "name": "common-js",
      "main": "./common-js.js"
    }
    
  3. Install this local package in your Cloud Functions root directory:
    npm install ./lib
    
  4. Now you can import it like any other standard npm package:
    const commonJs = require('common-js');
    

3. Ensure deployment includes the file

Double-check your .gitignore to make sure it doesn't exclude the lib/ directory or common-js.js. Firebase packages all non-ignored files during deployment, so this step is critical.

4. Test locally and deploy

  • Run firebase serve to confirm local execution works with the new setup.
  • If deployment still fails, use firebase deploy --only functions --debug to get detailed error logs—this will help you quickly pinpoint any remaining issues with paths or module compatibility.

内容的提问来源于stack exchange,提问作者David Vávra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:58:10