'"Unexpected token stripe" when trying to deploy Firebase Functions

I'm trying to incorporate Stripe into an iOS app using Firebase Functions. I'm following the Stripe documentation for "Accepting a payment" in Swift with Node backend. I first did npm install --save stripe. That finished with no errors. Then I did npm install. My index.js looks like this so far:

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

const functions = require('firebase-functions');
const stripe = require('stripe')('sk_test_...');

const paymentIntent = await stripe.paymentIntents.create({
  amount: 1099,
  currency: 'usd',
});
const clientSecret = paymentIntent.client_secret

When running firebase deploy I get: 11:29 error Parsing error: Unexpected token stripe. Line 11 char 29 in my file is the stripe.paymentIntents...

This is my first time using Firebase Functions or Stripe, so I'm at a loss here. I appreciate any help.

EDIT:

Here's the contents of my package.json file.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "stripe": "^8.55.0"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}


Solution 1:[1]

This error is because on the cloud environment the stripe library is not installed before you require it.

npm install does install the dependencies but in your local environment, to install them on the Cloud Functions envirornment you need to edit the package.json file from the Cloud Function.

This to add the dependencies that it will be required by the function.

This is done by adding the dependency section to the package.json file

It will lok something like:

{
  "name": "sample-name",
  "version": "0.0.1",
  "dependencies": {
    "escape-html": "^1.0.3",
    "stripe": "^8.24.0"
  }
}

EDIT

With this code it works on Cloud functions:

const stripe = require('stripe')('<My Secret Key>');

exports.helloWorld = (req, res) => {
  let paymentIntent = null;

  paymentIntent = stripe.paymentIntents.create({
  amount: 2000,
  currency: 'usd',
  description: 'My first payment',
});
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Apparently the issue was the await because HTTP Cloud Functions work on a Synchronous way

Solution 2:[2]

I see this question is old but I ran into this exact issue and couldn't find an answer.

I had my backend functions folder nested within my overall app and I had let firebase generate some files for me, including a lint configuration. So, I ended up with two lint config files in my project total. The firebase generated one tried to enforce double quotes and the one I created tried to enforce single quotes. I ended up just deleting the generated lint config and it works fine now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ders_76