note (tl:dr): everything works locally but not in lambda.
I have a lambda function in AWS and when I run the server locally everything works perfectly, the usage of algolia is being made inside a service, which is in an endpoint inside my server.
I tried installing #algolia/cache-common and it didn't help either.
Every call that is made to the lambda crashes the entire app because of this error.
is there any way to fix it?
the error is the following:
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '#algolia/cache-common'
Require stack:
/opt/nodejs/node_modules/algoliasearch/dist/algoliasearch.cjs.js
/opt/nodejs/node_modules/algoliasearch/index.js
/var/task/dist/api/v1/services/algolia.service.js
/var/task/dist/api/v1/handlers/jobs.handler.js
/var/task/dist/api/v1/controllers/jobs.controller.js
/var/task/dist/api/v1/v1.routes.js
/var/task/dist/api/routes.js
/var/task/dist/serverless.js
/var/runtime/UserFunction.js
/var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module '#algolia/cache-common'",
"Require stack:",
"- /opt/nodejs/node_modules/algoliasearch/dist/algoliasearch.cjs.js",
"- /opt/nodejs/node_modules/algoliasearch/index.js",
"- /var/task/dist/api/v1/services/algolia.service.js",
"- /var/task/dist/api/v1/handlers/jobs.handler.js",
"- /var/task/dist/api/v1/controllers/jobs.controller.js",
"- /var/task/dist/api/v1/v1.routes.js",
"- /var/task/dist/api/routes.js",
"- /var/task/dist/serverless.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:202:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" 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 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
the way I use the algolia is like the following:
const applicationId: any = config.get("ALGOLIA.APPLICATION_ID");
const apiKey: any = config.get("ALGOLIA.ADMIN_API_KEY");
const client = algoliasearch(applicationId, apiKey);
const index = client.initIndex("my-actual-index");
my lambda structure is a lambda and 3 module layers, which worked for every library I used, but doesn't work for algolia in particular. when I watch the lambda's packages I can see algolia related packages
I tried installing the exact package(#algolia/cache-common) and it didn't do anything,
also tried instally #types/algolia and didn't work as well.
is there anything I missed?
This was fixed, the problem was me splitting to layers, and skipping the first index in the loop.
When you "install" dependencies like #algolia/cache-common you are installing them locally.
Your installed dependencies are not automatically available on AWS Lambda. Like your application code, your dependencies need to be deployed as well.
That's why it works on your local machine but not in Lambda.
You did not write anything on how you deploy your code. Tools like AWS SAM or the serverless framework usually take care of not only deploying your application code but also it's dependencies.
So I imagine you are deploying by hand. That means that you most likely will have to also deploy your node_modules folder to AWS Lambda.
Your deployment ZIP archive should look like this:
node_modules/
index.js
The node_modules folder will have a lot of sub-folders etc and obviously, you can have more than one .js file. But for the purpose of this post we leave it at that.
It would definitely be helpful to see how you are building your service. Are the Algolia modules in your core service lambda or one of the layers? I don't know much about layers but I'm curious if you have the same issue if all dependencies are bundled into the service itself?
Related
I have an API key hidden in a file, I'm not using dotenv to do this, and I'm importing them into my javascript file via ES6 modules. Netlify allows build commands to build this when the app is deployed.
cd scripts && echo -e "const API_KEY = 'blahblahblah'; const HASH_KEY = 'blahblahblah'; const TIME_STAMP = 'ts=1'; const URL_ENDPOINT='blahblahblah';\n\nexport API_KEY, HASH_KEY, TIME_STAMP, URL_ENDPOINT;" > apikey.js
That is what I put into Netlify's deploy settings.
When I go to the deployed site, I get "Uncaught syntax error: unexpected token export".
The file is made correctly in the sources tab of the console. I'm at a loss for what I've done incorrectly, or what Netlify isn't interpreting correctly.
I found the solution:
I was missing brackets in the build command, and after that I had to update some links from HTTP: to HTTPS:.
Thanks for trying to help out.
I recently started using Firebase Functions for my Flutter project.
I saw that there is a code generator (firebase_functions_interop) that will transform your Dart code to Javascript code. This enables you to develop your cloud functions in Dart which is pretty nice.
But after developing my first cloud function and trying to deploy it to Firebase with the Firebase CLI, I constantly get the following error message:
Error: EPERM: operation not permitted, stat 'C:\PATH_TO_MY_FIREBASE_PROJECT\build\node\packages'
I'm new to npm & Firebase Functions so maybe I'm doing something fundamentally wrong.
It looks like something about permissions maybe? I don't know. But I also have full permissions on my workspace folder & have enabled VSCode as an Administrator
I have already tried to clean my npm cache & reinstalling my npm version.
Let me know if you need me to post anything else.
make sure you do not have the firebase emulator running! it will cause this error when trying to do a deployment to the firebase cloud.
I am running this code when I run node index.js command in CMD it works perfectly but same code is not running when I am using it into firebase cloud function. I might be wrong but not able to find the issue I am searching this issue for 1 week.
this code is working
stripe.customers.create({
email: 'testemail#gmail.com',
}).then((customer) => {
return null;
// functions.firestore.document('data/{card}/payment/{tokenid}').set(customer.id)
});
but this is not working
exports.StripeSource =functions.firestore.document('data/{card}/tokens/{tokenid}').onCreate((user) => {
return stripe.customers.create({
email: 'testemail#gmail.com',
}).then((customer) => {
return null;
// functions.firestore.document('data/{card}/payment/{tokenid}').set(customer.id)
});
});
fire base functions log is
{"#type":"type.googleapis.com/google.cloud.audit.AuditLog","status":
{"code":3,"message":"INVALID_ARGUMENT"},"authenticationInfo":
{"principalEmail":"name.dev#domain.tv"},"requestMetadata":{"requestAttributes":
{},"destinationAttributes":
{}},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.fun
ctions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/payment
bystripe/locations/us-central1/functions/StripeSource"}
visual studio code terminal report here
C:\Users\Waqas\AndroidStudioProjects\flutter_stripe_payment1\android\functions\functions>firebase deploy --only functions
=== Deploying to 'paymentbystripe'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint C:\Users\Waqas\AndroidStudioProjects\flutter_stripe_payment1\android\functions\functions
> eslint .
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (37.28 KB) for uploading
+ functions: functions folder uploaded successfully
i functions: updating Node.js 8 function StripeSource(us-central1)...
! functions[StripeSource(us-central1)]: Deployment error.
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 'stripe'
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:5:16)
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)
Functions deploy had errors with the following functions:
StripeSource
To try redeploying those functions, run:
firebase deploy --only functions:StripeSource
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
C:\Users\Waqas\AndroidStudioProjects\flutter_stripe_payment1\android\functions\functions>
You did not install stripe. I recommend you to look your dependencies in package.json and add "stripe":"^6.20.0". Any new version of this will do the trick.
Finally run Firebase deploy. I hope this helps.
This error means the deployment script could not find the stripe package. You probably have the stripe module installed globally but it is not referenced explicitly in your package.json for this project.
When you deploy firebase function the deployment script takes your package.json and pulls all of the dependencies. It will not look for dependencies installed locally on your machine.
Either add the stripe dependency manually to the package.json or run:
npm install stripe
then verify the stripe module has been added to your package.json file.
I'm trying to deploy my api to Google Cloud Functions, and I'm getting this:
EROFS: read-only file system, mkdir '/user_code/uploads'
⚠ functions[post]: Deployment error. Function load error:
Code in file index.js can't be loaded. Is there a syntax error in your code?
Detailed stack trace: Error: EROFS: read-only file system, mkdir '/user_code/uploads'
at Error (native)
at Object.fs.mkdirSync (fs.js:932:18)
at Function.sync (/user_code/node_modules/multer/node_modules/mkdirp/index.js:71:13)
at new DiskStorage (/user_code/node_modules/multer/storage/disk.js:21:12)
at module.exports (/user_code/node_modules/multer/storage/disk.js:65:10)
at new Multer (/user_code/node_modules/multer/index.js:15:20)
at multer (/user_code/node_modules/multer/index.js:95:12)
at Object.<anonymous> (/user_code/api/user.js:105:46)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
Everything in the Cloud Functions runtime is read-only except for os.tmpdir() (which is likely going to be /tmp, but you shouldn't assume that). If you have any code (in api/user.js for example) that attempt to write anywhere else, it'll error.
Same issue for python, but putting this for clarity. My error -
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 753, in download_to_filename
with open(filename, "wb") as file_obj:
OSError: [Errno 30] Read-only file system: 'testFile.zip'
Get the temp directory as follows (usually /tmp):
import tempfile
tmpdir = tempfile.gettempdir()
Googles documentation can be found here.
While Cloud Storage is the recommended solution for reading and writing files in App Engine, if your app only needs to write temporary files, you can use standard Python 3.7 methods to write files to a directory named /tmp.
All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory. In addition, files in the /tmp directory are only available to the app instance that created the files. When the instance is deleted, the temporary files are deleted.
Gen1 cloud functions are read-only systems. However, Gen2 cloud functions aren't. I'd recommend changing your function to Gen2
(be careful, this might interfere with another config as gen 2 can be considered as a cloud run)
My Node app is running fine locally, but has run into an error when deploying to Heroku. The app uses Sequelize in a /models folder, which contains index.js, Company.js and Users.js. Locally, I am able to import the models using the following code in /models/index.js:
// load models
var models = [
'Company',
'User'
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
});
This works fine, however, when I deploy to Heroku the app crashes with the following error:
Error: Cannot find module '/app/models/Company'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at module.exports.Sequelize.import (/app/node_modules/sequelize/lib/sequelize.js:219:24)
at module.exports.sequelize (/app/models/index.js:60:43)
at Array.forEach (native)
at Object.<anonymous> (/app/models/index.js:59:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
Process exited with status 8
Initially I thought it was due to case sensitivity (local mac vs heroku linux), but I moved the file, made a git commit, and then moved back and committed again to ensure Company.js is capitalized in the git repository. This didn't solve the problem and I'm not sure what the issue could be.
The problem was due to case sensitivity and file naming. Mac OS X is case insensitive (but aware) whereas Heroku is based on Linux and is case sensitive. By running heroku run bash from my terminal, I was able to see how the /models folder appeared on Heroku's file system. The solution was to rename User.js and Company.js on my local system to new temporary files, commit the changes to git, then rename back to User.js and Company.js being mindful of the capitalized first letter and then committing the changes again via git. Previously I had tried to rename the files directly from user.js to User.js and company.js to Company.js but the git commit and case-sensitive file name changes did not reflect on Heroku.
I can't see the exact fix, but you can figure it out yourself by running heroku run bash to log into a Heroku instance, then run node to enter a REPL, and try requiring the paths directly.
For me, it was caused by a folder that I had accidentally included in .gitignore!
I've been through an error like this one and the cause was that I renamed a module module.js to Module.js and the Heroku cache was conflicting the names. You must disable module caching to avoid this kind of error:
$ heroku config:set NODE_MODULES_CACHE=false
Source: https://help.heroku.com/TO64O3OG/cannot-find-module-in-node-js-at-runtime
One of my files had a lowercase name locally and it was required as a lowercase.
const Product = require('../models/product');
On the git repo it was capitalized.
'../models/Product'
The server was trying to require a file which did not exist. I had to use git mv to rename the file locally then reupload it to fix the issue.
Not sure if this is the same issue as described here, but for me my require("dotenv").config() was not conditioned upon the environment that the code was running in, thus Heroku could not find it since it was installed as a devDependency.
Fix:
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
For me, I just deleted the older app from Heroku and created the new one via Heroku web, and then pushed the code to the newer one, and then it worked.
For me what I changed was:
File name was CheckPermissions and I changed it to checkPermissions and then hosted. Error occurred.
Then revert the changes and hosted. This time worked well.
I faced the same issue and resolved using dockerizing my application.
Create dockerFile from node
set heroku stack as docker
Deploy
Ref : https://devcenter.heroku.com/categories/deploying-with-docker