Firebase Schedule Functions giving error when deploy - node.js

I was referring Firebase documentation for cloud function to schedule a function. But It was giving following error when try to deploy.
Error: Error occurred while parsing your function triggers.
token-refresh/functions/index.js:5
export scheduledFunction = functions.pubsub.schedule('5 23 * * *').onRun((context) => {
^^^^^^
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:743:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:810:10)
at Module.load (internal/modules/cjs/loader.js:666:32)
at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
at Function.Module._load (internal/modules/cjs/loader.js:598:3)
at Module.require (internal/modules/cjs/loader.js:705:19)
at require (internal/modules/cjs/helpers.js:14:16)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
at Module._compile (internal/modules/cjs/loader.js:799:30)
I just tried to deploy exact function that is in documentation, but it is giving error.
My Code I tried to deploy,
const functions = require('firebase-functions');
export scheduledFunction = functions.pubsub.schedule('5 23 * * *').onRun((context) => {
console.log('This will be run at 23.05 UTC');
});
My versions:
Firebase/ firebase-tools: 7.0.0
node: 11.11.0
npm: 6.7.0
Documentation
https://firebase.google.com/docs/functions/schedule-functions

The following should work:
const functions = require('firebase-functions');
exports.scheduledFunction = functions.pubsub.schedule('5 23 * * *').onRun((context) => {
console.log('This will be run at 23.05 UTC');
});
See https://firebase.google.com/docs/functions/get-started and https://cloud.google.com/functions/docs/writing/

Related

Have problem using export function in node js [duplicate]

This question already has answers here:
Node.js - SyntaxError: Unexpected token import
(16 answers)
Closed 1 year ago.
The code for pug.compiler.js is:
/**
* Pug Utilities
*/
const {compileFile} =require('pug');
/**
*
* #param {String} relativeTemplatePath Pug template name
* #param {Object} [data] Object
* #returns {String} HTML
*/
export function compile(relativeTemplatePath, data){
let absoluteTemplatePath = process.cwd() + '/src/views/' + relativeTemplatePath + '.pug';
let compiledTemplate = compileFile(absoluteTemplatePath)(data);
return compiledTemplate;
};
The error is :
export function compile(relativeTemplatePath, data){
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:984:16)
at Module._compile (internal/modules/cjs/loader.js:1032:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (D:\ProgramData\projects\nodejs\whatsapp\test\app.js:11:22)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
So I have a problem understanding how do I use this export function. I think it should work right?
The problem is NodeJS considers module by default as CommonJS Modules.
So instead of export you should use module.exports, or if you're using NodeJS v13+ you can enable ES Modules by either:
Changing the extension of your file to mjs
Adding "type": "module" property to your package.json
Using module.exports should be like:
function compile(relativeTemplatePath, data){
let absoluteTemplatePath = process.cwd() + '/src/views/' + relativeTemplatePath + '.pug';
let compiledTemplate = compileFile(absoluteTemplatePath)(data);
return compiledTemplate;
};
module.exports = {compile};

"unexpected token function" error for async, even though node version is 7.6.0+

I'm running out of ideas. I was running node 6.5.0, but then I needed to write some code that has async/await. So I updated node version to 7.7.0 (verified by running node -v) and tried running my app.js again. Unfortunately I'm getting the same error.
Do you have any idea what is happening? I'm running the app with a command node app.js and node -v gives me v7.7.0 response.
I hope I included enough information, please let me know if there's anything I could add to help diagnose this.
Error:
async function navigateWebsite() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:528:28)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/lucas/app/myapp/app.js:178:19)
at Module._compile (module.js:556:32)`
I'm pasting this function into my app.js application - not sure how code helps if it's clearly an issue with node and not the code.
function test() {
return new Promise(resolve => {
setTimeout(() => {
resolve('test');
}, 2000);
});
}
async function navigateWebsite() {
const msg = await test();
console.log('Message:', msg);
}

firebase-fuctions ,TypeError: Cannot read property 'onRequest' of undefined

updated firebase via npm i -g firebase-tools ,
used my firebase id ,
while deploying my function at firebase ( with firebase deploy ) this error results out
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'onRequest' of undefined
at Object.<anonymous> (C:\Users\umer\Desktop\learning chatbots\usingFulfilmentAsWebhook\functions\index.js:9:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:15:15
at Object.<anonymous> (C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:53:3)
here is the code
const functions = require('firebase-functions');
exports.webhook = functions.http.onRequest((request,response)=>{
console.log("request.body.result.parameters", request.body.result.parameters);
let params = request.body.result.parameters;
response.send({
speech: `${params.name} your hotel booking request for ${params.roomType} room is forwarded for ${params.persons} persons. We will contact you on ${params.email} soon`
});
});
try after putting "s" in http in below line
exports.webhook = functions.https.onRequest((request,response)=>{
...
}
Add this in your first line
const functions = require('firebase-functions');
Besides, make sure you have necessary packages installed.

Alexa Lambda Function "Unable to import module 'index'" when using new required function

I'm building an Alexa skill (deploying via CLI), and all has been going well up until needing to make a few Http calls.
I decided to use first axios, then require in order to do so.
When I installed axios using npm, all seemed well. Adding just the "const axios= require('axios');" line caused my lambda function to start complaining about "Unable to import module 'index'" in the Cloudwatch logs, however, and specifically calling out the line in index.js where I make that require statement.
Removed axios, tried require...same deal.
Any thoughts?
Not actually using the packages yet even, it complains on the require line if I uncomment it.
/* eslint-disable func-names */
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
const error_handler = require('./error_handler');
const globals = require('./globals');
const helper_functions = require('./helper_functions');
const intents_aquarium = require('./intents_aquarium');
const intents_built_in = require('./intents_built_in');
const intents_conversion = require('./intents_conversion');
const intents_helper = require('./intents_helper');
const intents_tank = require('./intents_tank');
const intents_unhandled = require('./intents_unhandled');
const skillBuilder = globals.Alexa.SkillBuilders.standard();
//const request = require('request');
exports.handler = skillBuilder
.addRequestHandlers(
intents_built_in.launchRequest,
intents_built_in.exitHandler,
intents_built_in.sessionEndedRequest,
intents_built_in.helpIntent,
intents_built_in.yesIntent,
intents_built_in.noIntent,
intents_aquarium.aquariumCreateIntentHandler,
intents_aquarium.aquariumCreateSimpleImperial,
intents_conversion.aquariumVolumeIntentGallonsToLitres,
intents_conversion.aquariumVolumeIntentLitresToGallons,
intents_helper.thankYou,
intents_tank.tankObservation,
intents_built_in.fallbackHandler,
intents_unhandled.unhandledIntent,
)
.addErrorHandlers(error_handler.errorHandler)
.withTableName('Tank-Mate')
.withAutoCreateTable(true)
.lambda();
Error looks like this:
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/index.js:16:17)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
...which to me looks like it's complaining about the index.js require line.

Visual Studio NPM Task Runner fails, but running task outside of VS runs fine

I'm working on bringing a ReactJS app over to a .NET setup. I'm trying to run an NPM script from the task runner (or anywhere within VS.) When I run my build command from the task runner, it fails due to an ES6 arrow function within one of my scripts. However, if I load up cmd.exe or powershell, navigate to the project and run it by hand, it runs perfectly.
Any idea why Visual Studio would be doing this?
The output looks something like this:
> cmd.exe /c npm run build --color=always
> ...cmd1
> ...cmd2
> mocha --reporter spec tools/testSetup.js "Scripts/app/**/*.test.js"
C:\Code\HAL\dev\Test\HALTest\HALReact\tools\testSetup.js:40
Object.keys(document.defaultView).forEach((property) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:217:14)
at Mocha.run (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:469:10)
at Object.<anonymous> (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\bin\_mocha:404:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
The file that it errors on basically just sets up JSDOM:
process.env.NODE_ENV = 'test';
// Register babel so that it will transpile ES6 to ES5
// before our tests run.
require('babel-register')();
// Disable webpack-specific features for tests since
// Mocha doesn't know what to do with them.
require.extensions['.css'] = function () {return null;};
require.extensions['.png'] = function () {return null;};
require.extensions['.jpg'] = function () {return null;};
// Configure JSDOM and set global variables
// to simulate a browser environment for tests.
var jsdom = require('jsdom').jsdom;
var exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
documentRef = document; //eslint-disable-line no-undef
Again, I can run this from the CLI directly, and it will work. It has babel-register invoked. I'm just not sure why it wants to error here. If I swap out the arrow function with a regular function, I get other errors in my tests, so it's like the task runner is not able to find babel, or is losing some environmental setting.

Resources