35

我尝试部署一个 firebase 功能,但由于 ESLint 的一些问题,它一直失败。我也试过用谷歌搜索答案,但我看到的一切对我来说都是希腊语。

我也偶然发现了这个这个答案,但似乎它们不适用于我的场景。

我的功能

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });


//import firebase functions modules
const functions = require("firebase-functions");
//import admin module
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref("/Message").onWrite( event => {
    //  Grab the current value of what was written to the Realtime Database.
    var valueObject = event.data.val();
    // Log the event
    console.log("Push notification event triggered for " + valueObject.title);

    //if(valueObject.title != null) {
      //valueObject.photoUrl= "Sent you a photo!";
   // }

  // Create a notification
    const payload = {
        data: {
            body:valueObject.body,
            imgUrl: valueObject.imgUrl,
            postUrl: valueObject.postUrl,
            title: valueObject.title
        },
    };

  //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };


    return admin.messaging().sendToTopic("Posts", payload, options);
});

日志

0 info it worked if it ends with ok
1 verbose cli [ '/home/xoxo/.nvm/versions/node/v9.5.0/bin/node',
1 verbose cli   '/home/xoxo/.nvm/versions/node/v9.5.0/bin/npm',
1 verbose cli   '--prefix',
1 verbose cli   '/home/xoxo/Android/Projects/FirebaseFunctions/functions',
1 verbose cli   'run',
1 verbose cli   'lint' ]
2 info using npm@5.6.0
3 info using node@v9.5.0
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /home/xoxo/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/xoxo/Android/Projects/FirebaseFunctions/functions/node_modules/.bin:/home/xoxo/.nvm/versions/node/v9.5.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
9 verbose lifecycle functions@~lint: CWD: /home/xoxo/Android/Projects/FirebaseFunctions/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'eslint .' ]
11 info lifecycle functions@~lint: Failed to exec lint script
12 verbose stack Error: functions@ lint: `eslint .`
12 verbose stack spawn ENOENT
12 verbose stack     at ChildProcess.<anonymous> (/home/xoxo/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:48:18)
12 verbose stack     at ChildProcess.emit (events.js:160:13)
12 verbose stack     at maybeClose (internal/child_process.js:943:16)
12 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
13 verbose pkgid functions@
14 verbose cwd /home/xoxo/Android/Projects/FirebaseFunctions
15 verbose Linux 4.13.0-32-generic
16 verbose argv "/home/xoxo/.nvm/versions/node/v9.5.0/bin/node" "/home/xoxo/.nvm/versions/node/v9.5.0/bin/npm" "--prefix" "/home/xoxo/Android/Projects/FirebaseFunctions/functions" "run" "lint"
17 verbose node v9.5.0
18 verbose npm  v5.6.0
19 error file sh
20 error code ELIFECYCLE
21 error errno ENOENT
22 error syscall spawn
23 error functions@ lint: `eslint .`
23 error spawn ENOENT
24 error Failed at the functions@ lint script.
24 error This is probably not a problem with npm. There is likely additional logging output above.
25 verbose exit [ 1, true ]

控制台输出

xoxo@xo9:~/Android/Projects/FirebaseFunctions$ firebase deploy

=== Deploying to 'testproject-a1bda'...

i  deploying functions
Running command: npm --prefix $RESOURCE_DIR run lint

> functions@ lint /home/xoxo/Android/Projects/FirebaseFunctions/functions
> eslint .

sh: 1: eslint: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! functions@ lint: `eslint .`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/xoxo/.npm/_logs/2018-02-03T22_35_55_608Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

包.js

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "./node_modules/.bin/eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.8.1",
    "firebase-functions": "^0.8.1"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
}

在此处输入图像描述

请问我做错了什么?

4

7 回答 7

60

我可以想象您的项目进入这种状态的唯一方法是,如果您在项目创建时拒绝允许 npm 安装依赖项。所以当你问这个问题时:

? 你想现在用 npm 安装依赖项吗?

您可能已经回答“不”。这将阻止安装 eslint 包,并且您会看到该错误。

否则,您可能已经以某种方式删除了 eslint 安装的包node_modules。您可以npm install从该functions文件夹运行以重新创建node_modules.

于 2018-02-03T23:12:11.937 回答
35

轻松修复。在您的 package.json 更改中

> "lint": "eslint ."

"lint": "eslint",
于 2021-07-17T19:22:50.553 回答
19

有同样的问题,我重新安装了 npm

npm install npm -g

然后重新启动 Firebase 功能(您必须覆盖所有文件并确保安装 EsLine 调试并安装所有依赖项)是的,任何请求都按 Y

Firebase init functions

再次部署后一切正常

Firebase deploy
于 2019-08-15T12:46:27.147 回答
13

如果您在部署 firebase 项目时发现 -prefix "%REASOURCE_DIR%" 运行初始化类型错误,那么您必须在 firebase.json 文件中编辑一些代码。这是一个例子在此处输入图像描述

于 2020-04-17T10:36:58.877 回答
4

嘿,你只需要从 then 语句中返回一些东西

let params = {...};
var firestore = admin.firestore();
var dbRef = firestore.collection('your-collection-name');

dbRef.add(params)
     .then((docRef) => {
       console.log("doc added with ID ", docRef.id);

       return null
     })
     .catch((err) => {
       console.error("Error in adding document ", err);
     });
于 2018-03-24T12:37:11.007 回答
1

我出现了类似的错误。我发现保存 firebase 项目的目录名为 firebase/database。我删除了 / 并将目录名称设为 firebase-database。希望这可以帮助某人。

于 2019-04-02T23:24:13.303 回答
0

我在 Windows 上遇到了类似的 ESLINT 问题

我只是为了调用 firebase deploy 命令而破坏了 npm run deploy 。这个调用允许我查看“firebase deploy”命令执行之外的错误,有趣的是 Eslint 与文件有 CLRF 冲突

然后我运行 npm run lint -- --fix 并且这些问题得到了解决

:D

于 2021-01-20T06:03:54.657 回答