Function load error: Code in file index.js can't be loaded. firebase functions issue - node.js

I am trying to deploy new functions in my firebase project but it started showing an error. I have 5 functions in my index.js file and at any given point, 4 functions are getting deployed and one is throwing this error.
⚠ functions[whenUserIsAdded]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase-functions'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:1:79)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
This happens every time to any one of the 5 functions and this happens in random. I deployed the same code about 2 hours ago and it went smooth. Now after adding some console.log statements, it is showing this error at random with a function.
Can anybody help me with this issue. Thanks in advance.
Also, I am on the blaze plan.

Ok. I think I found the issue. I was having firebase-functions V0.6.3. I believe that was depreciated. Hence it was not working I updated my package.json to the following and it worked. Because functions is still in Beta, I dont think we can do anything about this.
"firebase-admin": "~5.4.2",
"firebase-functions": "^0.7.1",

Related

firebase functions, javascript: how to test locally if all packages are specified in package.json

I get this error message when deploying my firebase function:
Function execution started
Provided module can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '#docsndata/testing-stub-graphql'
Require stack:
- /workspace/local_deps/workflow/core2/dist/cjs/recordManagement/StubRecordManagement.js
- /workspace/local_deps/workflow/core2/dist/cjs/index.js
- /workspace/local_deps/cyclepositive/webhook2/dist/cjs/onWebBooking.js
- /workspace/local_deps/cyclepositive/webhook2/dist/cjs/index.js
- /workspace/lib/index.js
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/#google-cloud/functions-framework/build/src/loader.js
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/#google-cloud/functions-framework/build/src/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (/workspace/local_deps/workflow/core2/dist/cjs/recordManagement/StubRecordManagement.js:9:32)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
Could not load the function, shutting down.
I know what the root cause is - I have a dependency that is not in package.json
My question is: how do I test this locally? When I do this:
firebase emulators:start --only functions
my function starts. So it seems like the deploy process of the function does something different, like actively check for missing packages. How can I check I have that right, before even attempting to deploy?
Edit #1 Rewording and retagging to reflect fact that am using firebase functions, not google cloud functions directly.
Edit #2 I tried deploying again this morning, after three of the above failures last night, and now it works. Is the deployment process flaky? Regardless, question still stands: what is the best way to test the firebase deploy process locally?

how can i fix this error that nodemon keep on showing

i following an express.js tutorial andd after some code and functionalities with api requests i followed exactly how he changed the code that handles these requests into a seperate js file in another folder just to clean up the code but after i did that this error showed up
i tried changing the file name , i tried removing the node-modules folder and running npm install again and i tried installing the module with the issue
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/routes/api/members'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\Get_Rico\Desktop\crashcoursse\index.js:11:25)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[nodemon] app crashed - waiting for file changes before starting...
i expect nodemon to save the changes and work just fine with no errors just like what happened in the tutorial
It appears the error is probably coming from this line of code and it's telling you that it can't find the '/routes/api/members' module file.
And, this would not be working because you're trying to load a local module without a proper path name. If no path is specified, then it tries to load a global or built-in module. If that module is in the current directory, then you should use the correct path:
const yourModule = './routes/api/members';

Google Cloud Function not recognizing Papaparse 5.0 Dependency

I am building a Node.js Google Cloud function, and it is not recognizing the dependency "papaparse":
in package.json
"dependencies": {
"papaparse": "^5.0.0",
"twilio": "^3.33.2"
}
Error code I am getting:
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'papaparse'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/index.js:8:14)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
I have tried reinstalling papaparse, running npm audit, but those steps haven't helped.
Does anyone know why this would happen?
I just managed to deploy a Cloud Function with papaparse.
For this you just need to deploy your function from the Cloud Shell. First create a folder where you gonna insert all your files. Then clone the repo from the papaparse github with the command git clone into that folder.
Now after adding your Index.json and your Package.json you can deploy your Cloud Function following this Deploying from Your Local Machine documentation.

Serverless Node TypeScript app can't import module

I'm tired of this error! This is Serverless app with Lambda in AWS. Locally it works perfectly. Testing locally didn't work for load of other reasons. I'm testing in staging. Yet I'm getting this error:
Unable to import module 'src/engine/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>(/var/task/src/engine/controllers/image.js:43:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I followed all the steps advised elsewhere including: moving all dependencies to dependencies (out of dev), installing serverless and all. I'm using Node 6.10.3 and TypeScript 3.1. Can you help?
If you're trying host your nodejs projects which has so many dependencies not related to aws services inside, you better zip your projects in S3 first then import lambda from S3.

Trouble getting Mocha, node.js and WebStorm to work with each other

I have been following the instructions to install Mocha with WebStorm found here: https://www.jetbrains.com/help/webstorm/2017.1/testing-javascript-with-mocha.html#d194239e92
I have set the configurations to run files that match the pattern: **/*.spec.js
node interpreter: /usr/bin/node
working directory:/home/web/app
mocha package:/home/web/app/node_modules/mocha
I am writing in TypeScript which is converted to ES6, the output of which is what I'm attempting to test. However I get this error which I don't understand and so don't really know where to begin looking to resolve.
I don't understand why it is looking for "ts-node", considering it's only supposed to look for "spec.js" files. and past that, I don't understand why the folder it is looking for in ts-node is missing. I have uninstalled then re-installed ts-node a couple times, neither of which yielded the sought after folder... I have no clue as to how to proceed or even what further information is needed to help resolve this issue.
Any help would be appreciated. I just want to test my application and this is proving to be a real headache.
Here is the error:
/usr/bin/node /home/web/app/node_modules/mocha/bin/_mocha --ui bdd
--reporter /opt/webstorm/ws/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js
**/*.spec.js TypeError: The specified path does not exist: /home/web/app/node_modules/ts-node/tests
at resolveSync (/home/web/app/node_modules/ts-node/node_modules/tsconfig/src/tsconfig.ts:72:9)
at Object.loadSync (/home/web/app/node_modules/ts-node/node_modules/tsconfig/src/tsconfig.ts:140:16)
at readConfig (/home/web/app/node_modules/ts-node/src/index.ts:418:18)
at Object.register (/home/web/app/node_modules/ts-node/src/index.ts:176:18)
at Suite. (/home/web/app/node_modules/ts-node/src/index.spec.ts:190:5)
at Object.create (/home/web/app/node_modules/mocha/lib/interfaces/common.js:114:19)
at context.describe.context.context (/home/web/app/node_modules/mocha/lib/interfaces/bdd.js:44:27)
at Suite. (/home/web/app/node_modules/ts-node/src/index.spec.ts:189:3)
at Object.create (/home/web/app/node_modules/mocha/lib/interfaces/common.js:114:19)
at context.describe.context.context (/home/web/app/node_modules/mocha/lib/interfaces/bdd.js:44:27)
at Object. (/home/web/app/node_modules/ts-node/src/index.spec.ts:13:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at /home/web/app/node_modules/mocha/lib/mocha.js:230:27
at Array.forEach (native)
at Mocha.loadFiles (/home/web/app/node_modules/mocha/lib/mocha.js:227:14)
at Mocha.run (/home/web/app/node_modules/mocha/lib/mocha.js:495:10)
at Object. (/home/web/app/node_modules/mocha/bin/_mocha:460:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:390:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:505:3
Process finished with exit code 1
I figured it out, my path was from the root of my project, which contained node_modules, and so the node_modules directory was being tested, causing errors from tests inside the ts-node folder. the fix was to exclude node_modules directory from the specified path for test modules.

Resources