fire base cloud function is not working in node.js file - javascript

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.

Related

Cannot require nodejs files in electron worker process

I am running a worker on a page of electron after a button click which runs the beginning part of the file just fine. But it gives me the error:
node:internal/modules/cjs/loader:940 Uncaught Error: Cannot find module './bar'
Require stack:
- electron/js2c/worker_init
at Module._resolveFilename (node:internal/modules/cjs/loader:940:15)
at o._resolveFilename (node:electron/js2c/worker_init:33:1095)
at Module._load (node:internal/modules/cjs/loader:785:27)
at c._load (node:electron/js2c/asar_bundle:5:13343)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at 3a43803….js:21:5
foo.js is in the same directory as bar.js which is being run as a worker.
I checked the location of the directory and it shows:
C:\Users\programmer\project\node_modules\electron\dist\resources\electron.asar\worker
I expect the locaction to be:
C:\Users\programmer\project\dist\src
P.S. I researched further and found this but the solution for it did not work for me despite being my exact problem.

Next.js doesn't load jsx

I have been searching about Next.js, the javascript framework of the moment, and I want to begin to practice. The problem is: it doesn't run in my computer by any means.
The natural form on creating a next project is with the command line:
npx create-next-app
or
yarn create next-app
The result is a template project with a file structure and a few scripts to run the server. The most basic script runs the server in development mode:
npm run dev
or
yarn dev
The result in terminal is the warning:
$ yarn dev
yarn run v1.22.5
$ next dev
(node:5348) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution
of the package at C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\node_modules\postcss\package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
ready - started server on http://localhost:3000
error - C:/Users/maths/Documents/codigos/node/my-app/pages/_app.js 4:9
Module parse failed: Unexpected token (4:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. S
ee https://webpack.js.org/concepts#loaders
|
| function MyApp({ Component, pageProps }) {
> return <Component {...pageProps} />
| }
|
And when I enter the website in localhost:3000, the page on browser shows:
Internal Server Error
After making the http requisition from the browser, the terminal shows the following message:
event - build page: /next/dist/pages/_error
wait - compiling...
error - C:/Users/maths/Documents/codigos/node/my-app/pages/_app.js 4:9
Module parse failed: Unexpected token (4:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. S
ee https://webpack.js.org/concepts#loaders
|
| function MyApp({ Component, pageProps }) {
> return <Component {...pageProps} />
| }
|
Error: Cannot find module 'C:\Users\maths\Meus Documentos\codigos\node\my-app\.next\server\pages-manifest.json'
Require stack:
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\load-components.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\api-utils.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-server.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\next.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\lib\start-server.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\cli\next-dev.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\bin\next
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at getPagePath (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js:1:
657)
at requirePage (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js:1:
1062)
at loadComponents (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\load-compo
nents.js:1:807)
at DevServer.findPageComponents (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\ser
ver\next-server.js:74:296)
at DevServer.renderErrorToHTML (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\serv
er\next-server.js:126:120)
at DevServer.renderErrorToHTML (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\next-dev-
server.js:34:974)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
at async DevServer.render (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\ne
xt-server.js:72:236)
at async Object.fn (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-serv
er.js:56:618)
at async Router.execute (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\rout
er.js:23:67)
at async DevServer.run (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-
server.js:66:1042)
at async DevServer.handleRequest (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\se
rver\next-server.js:34:1081) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\require.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\load-components.
js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\api-utils.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\next-server.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\server\\next.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\server\\lib\\start-server.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\cli\\next-dev.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\bin\\next'
]
}
I have tried several things to fix it, create the next project without the script from npx and yarn (by manually installing the dependencies), update versions of node and npm, etc. It seems that the program can't load the jsx, the first error points to the moment when the first jsx component appear in the code. The message says that webpack isn't configured with a valid loader to this code.
Environment:
Win 10
npm v7.3.0
node v15.5.0
next v10.0.4
react v17.0.1
react-dom v17.0.1
Downgrade your version of Nodejs to 14 or 12 Node version 15 will break your environment. If you want to still want to use 15 get node version switcher and switch the version based on the projects your working on.

Node.js are not running

Using macOs High Sierra, trying to run small node.js code node server.js, from iTerm, showing this error:
module.js:545
throw err;
^
Error: Cannot find module '/Users/vikasenkova/server.js'
at Function.Module._resolveFilename (module.js:543:15)
at Function.Module._load (module.js:470:25)
at Function.Module.runMain (module.js:690:10)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
Cant't find solution here.. if somebody knows..Found this link https://gist.github.com/DanHerbert/9520689 when trying to delete files with this: rm -rf /usr/local/lib/node_modules showing that permission denied. Nothing helps.
Check if your running in the same directory as the server.js file. Also update your question with some code to get more specific answer. Hope it helps you.

Arrow function from meteor build is crashing heroku deploy

I have deployed a js application on heroku that uses meteor. The build works on localhost but fails on the heroku server. I checked the logs and found this:
.js:312:12)
2017-03-18T03:29:07.070711+00:00 app[web.1]: const unicodeNormalizePath = (path) => {
2017-03-18T03:29:07.074825+00:00 app[web.1]: at Module.require (module.js:364:17)
2017-03-18T03:29:07.074821+00:00 app[web.1]: SyntaxError: Unexpected token >
2017-03-18T03:29:07.074824+00:00 app[web.1]: at Module.load (module.js:356:32)
2017-03-18T03:29:07.070741+00:00 app[web.1]: ^
This gave me the thought that it was not compatible with ES6 arrow functions so I installed ecmascript for meteor to make it compatible with ES5.
This has not solved the problem and this arrow function is still crashing the web app.
Any suggestions on how to stop this?
I had the same problem using, probably, an old buildpack for Heroku. I switched to the buildpack https://github.com/AdmitHub/meteor-buildpack-horse.git and it solved the problem perfectly.
Working buildpack:
heroku buildpacks:set https://github.com/AdmitHub/meteor-buildpack-horse.git
I also disabled the previous buildpack by typing:
heroku config:unset LINK_TO_OLD_BUILDPACK_REPO
The old one I used was https://github.com/jordansissel/heroku-buildpack-meteor.git
May be you can use babel to transform es6 to es5

Heroku + Node: Cannot find module error

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

Categories