【问题标题】:ENOENT error while deploying Cloud Functions to Firebase将 Cloud Functions 部署到 Firebase 时出现 ENOENT 错误
【发布时间】:2020-05-10 14:14:50
【问题描述】:

我正在使用 Cloud Functions 在实时数据库更新时向用户发送通知。

当我使用 firebase init functions 创建项目并运行 Windows 10 时,我选择了“javascript”。

每当我使用firebase deploy 时,它都会向我显示此错误:

Error: spawn npm --prefix "I:\Indian Meme Templates\functions" run lint ENOENT
    at notFoundError (C:\Users\Pranav\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
    at verifyENOENT (C:\Users\Pranav\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
    at ChildProcess.cp.emit (C:\Users\Pranav\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:25)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess.cp.emit (C:\Users\Pranav\AppData\Roaming\npm\node_modules\firebase- 
     tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:30:37)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
{
    code: 'ENOENT',
    errno: 'ENOENT',
    syscall: 'spawn npm --prefix "I:\\Indian Meme Templates\\functions" run lint',
    path: 'npm --prefix "I:\\Indian Meme Templates\\functions" run lint',
    spawnargs: []
}

这是我的Index.js 文件:

const functions = require('firebase-functions');
exports.sendAdminNotification = functions.database.red('\Data/{pushId}').onWrite(evet =>){

    const data = event.data.val();
    if(data.priority==1){
        const payload = { notification: 
            {title: 'New Data',
             body: '${data.title}'
            }
        };
        return admin.messaging().sendToTopic("News",payload).then(function(response))
        {
            console.log('Notification sent successfully: ',response);
        }).catch(function(error)){
            console.log('Notification sent failed: ',error);
        });
    }
});

【问题讨论】:

    标签: firebase google-cloud-functions firebase-cloud-messaging firebase-cli


    【解决方案1】:

    此错误的原因是在部署期间,firebase deploy 操作正在尝试 lint(校对)您的代码,但它找不到用于 lint 代码的工具和/或文件。

    尝试解决此问题的第一步是确保您已使用npm run install 将所有依赖项安装在functions 文件夹中。这将确保安装了eslint 工具。

    接下来,因为npm run lint 命令默认设置为调用eslint .,所以您的Index.js 文件应重命名为index.js。这是因为index.js 在 Javascript/Typescript 项目中作为文件夹的默认入口点具有特殊含义。大多数工具都希望遵循命名约定并且它是小写的。

    接下来,您提供的 index.js 文件有许多拼写错误(应该在 lint 工具正常工作时突出显示)。

    这是相同的代码,没有那些拼写错误:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();
    
    exports.sendAdminNotification = functions.database.ref('/Data/{pushId}').onWrite(event => {
        const data = event.data.val();
        if (data.priority == 1) {
            const payload = {
                notification: {
                    title: 'New Data',
                    body: '${data.title}'
                }
            };
    
            return admin.messaging().sendToTopic("News", payload)
                .then(function(response) {
                    console.log('Notification sent successfully: ', response);
                })
                .catch(function(error) {
                    console.log('Notification sent failed: ', error);
                });
        }
    });
    

    如果您刚开始使用 Firebase,我建议您使用 VS CodeAtom 等 IDE,因为它可以帮助您进行代码提示、实时 linting 和语法高亮显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 2022-08-19
      • 2021-09-27
      • 2019-04-11
      • 2018-12-02
      • 1970-01-01
      • 2021-08-14
      • 2018-05-25
      相关资源
      最近更新 更多