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

运行firebase init functions命令时遇npm错误,求解决方案

解决firebase init functions执行时的protobufjs npm错误

执行firebase init functions时触发如下npm错误,已重装npm(v8.19.4)和node(v18.5.0)但问题依旧。

错误日志

npm ERR! code 2
npm ERR! path /home/ramon/apicloud/node_modules/protobufjs
npm ERR! command failed
npm ERR! command /home/ramon/.cache/firebase/runtime/shell -c -- node scripts/postinstall
npm ERR! /bin/sh: 0: Illegal option --

npm ERR! A complete log of this run can be found in:
npm ERR! /home/ramon/.npm/_logs/2024-01-25T02_16_00_381Z-debug-0.log

错误栈详情

3116 verbose stack Error: command failed
3116 verbose stack     at ChildProcess.<anonymous> (/snapshot/firepit/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:63:27)
3116 verbose stack     at ChildProcess.emit (node:events:537:28)
3116 verbose stack     at maybeClose (node:internal/child_process:1091:16)
3116 verbose stack     at ChildProcess._handle.onexit (node:internal/child_process:302:5)
3117 verbose pkgid protobufjs@7.2.6
3118 verbose cwd /home/ramon/apicloud
3119 verbose Linux 6.5.0-14-generic
3120 verbose node v18.5.0
3121 verbose npm  v8.19.4
3122 error code 2
3123 error path /home/ramon/apicloud/node_modules/protobufjs
3124 error command failed
3125 error command /home/ramon/.cache/firebase/runtime/shell -c -- node scripts/postinstall
3126 error /bin/sh: 0: Illegal option --
3127 verbose exit 2
3128 timing npm Completed in 51906ms
3129 verbose unfinished npm timer reify 1706148960471
3130 verbose unfinished npm timer reify:build 1706149011539
3131 verbose unfinished npm timer build 1706149011540
3132 verbose unfinished npm timer build:deps 1706149011540
3133 verbose unfinished npm timer build:run:postinstall 1706149011561
3134 verbose unfinished npm timer build:run:postinstall:node_modules/protobufjs 1706149011561
3135 verbose code 2
3136 error A complete log of this run can be found in:
3136 error     /home/ramon/.npm/_logs/2024-01-25T02_16_00_381Z-debug-0.log

技术解决方案

1. 修复shell环境兼容性

错误提示/bin/sh: 0: Illegal option --,说明Firebase调用的shell不支持--参数格式:

  • 查看当前默认shell:
    echo $SHELL
    
  • 如果默认是dash(部分Linux发行版默认替换bash),切换为bash:
    chsh -s /bin/bash
    
  • 重启终端后重新执行firebase init functions。

2. 跳过postinstall脚本手动安装依赖

  • 执行firebase init functions时,选择拒绝自动安装依赖
  • 进入functions目录,执行:
    npm install --ignore-scripts
    npm rebuild protobufjs
    

3. 降级protobufjs版本

在项目根目录package.json中指定兼容版本,执行npm install:

"dependencies": {
  "protobufjs": "^7.1.2"
}

4. 清理缓存后重新初始化

  • 清理Firebase CLI缓存:
    firebase clear
    
  • 清理npm缓存:
    npm cache clean --force
    
  • 删除项目目录下的node_modules、package-lock.json文件,重新执行firebase init functions。

Firebase Cloud Functions项目初始化步骤(中文翻译)

  1. 运行firebase login,通过浏览器登录并验证Firebase CLI;
  2. 进入Firebase项目目录;
  3. 运行firebase init firestore,可接受Firestore规则和索引文件的默认值;若项目尚未使用Cloud Firestore,需按《Cloud Firestore快速入门》选择启动模式和位置;
  4. 运行firebase init functions,CLI会提示选择现有代码库或新建并命名代码库;入门阶段选择默认位置的单个代码库即可,后续扩展时可按代码库组织函数;
    CLI支持JavaScript和TypeScript两种语言(详见《使用TypeScript编写函数》),本教程选择JavaScript;
    CLI会询问是否用npm安装依赖,若选择拒绝,需在模拟或部署函数前运行npm install。

内容的提问来源于stack exchange,提问作者Ramon Basilio

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 15:51:07