javascript - NodeJS(Firebase): Cannot import custom class

标签 javascript node.js firebase google-cloud-functions

我正在开发用于发送推送通知的 Firebase 服务器端代码。为了使代码简洁,我决定将一些函数移动到我创建的另一个类中,并将其命名为 notificationFunctions.js

当我在 index.js 顶部执行 const notificationFunctions = require('notificationFunctions'); 并从 sendNotification< 中调用该函数时 函数,将项目部署到云端时出现错误:

⚠  functions[sendNotifications]: Deployment error.
Build failed: exit status 1
npm ERR! Linux 4.4.0-108-generic
npm ERR! argv "/nodejs/bin/node" "/nodejs/bin/npm" "--global-style" "--production" "--fetch-retries=5" "--fetch-retry-factor=2" "--fetch-retry-mintimeout=1000" "install" "/workspace"
npm ERR! node v6.11.5
npm ERR! npm  v3.10.10
npm ERR! code E404

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/notificationFunctions
npm ERR! 404 
npm ERR! 404  'notificationFunctions' is not in the npm registry.
npm ERR! 404 Your package name is not valid, because 
npm ERR! 404  1. name can no longer contain capital letters
npm ERR! 404 It was specified as a dependency of 'functions'
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:
npm ERR!     /workspace/npm-debug.log



Functions deploy had errors. To continue deploying other features (such as database), run:
    firebase deploy --except functions

我还在我的 package.json 中添加了 "notificationFunctions": "1.0.0" 并将自定义函数移至 node_modules 文件夹.

向 Firebase NodeJS 添加自定义类的正确方法是什么?

编辑:

当我遵循 Doug Stevenson 的建议并从 package.json 中删除自定义类,并将该类移动到与 index.js 相同的目录时,我仍然不断收到错误:

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error parsing triggers: Cannot find module 'notificationFunctions'

Try running "npm install" in your functions directory before deploying.

编辑2

好的。我想我必须更改将类导入到 require('./notificationFunctions') 的方式,这样可以消除部署项目时的所有错误。

但是,一旦我在云上运行代码,就会收到此错误:FIREBASE 警告:用户回调引发异常。类型错误:notificationFunctions.getNotificationPayload不是函数

我从代码中调用该函数的方式是:const Payload = notificationFunctions.getNotificationPayload(userLanguage, senderName, groupName, messageContent, messageType);

最佳答案

如果您将代码添加到函数文件夹中的项目中,则无需对 package.json 依赖项进行任何更改。仅发布到 NPM registry 的模块才需要 npm 依赖项.

您的构建现在正在做的是在 NPM 上寻找名为“notificationFunctions”的 Node 模块,而该模块显然不存在。您应该直接将模块代码与所有其他代码放在 functionsrequire() 下。

此外,请记住,node_modules 未随您的代码一起部署。 Cloud Functions 将获取服务器端的所有 npm 依赖项并使它们可供您的代码使用。

关于javascript - NodeJS(Firebase): Cannot import custom class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48311967/

相关文章:

firebase - 动态链接域不可用

ios - Firebase 存储错误

javascript - 设置 DIV 高度,使其到达视口(viewport)底部

javascript - 区分app调用$http和Angular在拦截器中请求静态资源

javascript - React-Redux/Jest 不变违规 : Could not find "store"

javascript - 如何使用nodejs和couchdb进行分页

node.js - jest/ts/tests 找不到名称 'x'

Swift Firebase 观察 .childAdded 检索重复数据

javascript - setState 在 componentDidMount() 中不起作用 - React

javascript - Node.js EasyRTC 视频录制