Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix 'parsing error: Unexpected token =>' in node?

I am using firebase functions for stripe payment integration. This particular function used for register customer with stripe.

Node version 10.15.3 ,

npm version 6.9.0 ,

"ecmaVersion": 6 in .eslintrc.json

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')(functions.config().stripe.testkey)

exports.createStripeCustomer = functions.auth.user()

          .onCreate(async (user) => {
             const customer = await
             stripe.customers.create({email: user.email});
             await admin.firestore()
               .collection('stripe_customers')
               .doc(user.uid)
               .set({customer_id: customer.id});

           });

The code is same as the firebase platform provide on github example https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js

Parsing error: Unexpected token =>

and if I change the "ecmaVersion": 6 to "ecmaVersion": 8 in .eslintrc.json

then error is .onCreate(async (user) => {

                            ^

SyntaxError: Unexpected token (

I want to deploy function properly so that user can register on stripe and date store in firebase storage

like image 320
Rav Avatar asked May 08 '19 07:05

Rav


People also ask

What does parsing error unexpected token mean?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What causes parsing error?

Here are some of the most common causes of the Android parse error: The app is not compatible with your device. Your phone does not have permission to install the app. The file you are trying to install is corrupt, incomplete, or damaged.

How do I resolve a parsing error in ESLint?

The Best Answer is. Unexpected token errors in ESLint parsing occur due to incompatibility between your development environment and ESLint's current parsing capabilities with the ongoing changes with JavaScripts ES6~7. The solution is to have ESLint parsed by a compatible parser.


1 Answers

Make sure you are running in your local machine node >= 8. For deployment, you should have in your package.json.

{
    //...
    "engines": {
        "node": "8"
    },
    //...  
}

For eslint, to enable parsing for async functions, you should include this in your config:

{
    "parserOptions": {
        "ecmaVersion": 2017
    }
}
like image 186
ajorquera Avatar answered Oct 04 '22 16:10

ajorquera