1

Windows is telling me I have a syntax error in my script, when I try to run it windows host says there is a syntax error at line 1 char 1, after checking and double checking I couldn't find the error, so I tried creating a script that just prints hello world console.log("hello world"); and this fails with the same error so I'm confused as to whether the issue is with the code or with windows host (my machine) and my javascript knowledge is basically non existent here is the script

const  algoliasearch = require('algoliasearch')
const  dotenv = require('dotenv')
const  firebase = require('firebase');
const  firestore = require('firebase/firestore');
// load values from the .env file in this directory into process.env
dotenv.load();
// initializes the firebase database.
firebase.initializeApp({
projectId: process.env.FIREBASE_PROJECT_ID,
databaseURL: process.env.FIREBASE_DATABASE_URL
})
const db = firebase.firestore();
// configure algolia
const algolia = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);
var docRef = db.collection(process.env.ALGOLIA_INDEX_NAME);
const records = [];
db.collection(process.env.ALGOLIA_INDEX_NAME).get()
.then((snapshot) => {
    snapshot.forEach((doc) => {
        // get the key and data from the snapshot
        const childKey = doc.id;
        const childData = doc.data();
        // We set the Algolia objectID as the Firebase .key
        childData.objectID = childKey;
        // Add object for indexing
        records.push(childData);
        console.log(doc.id, '=>', doc.data());
    });
    // Add or update new objects
    index.saveObjects(records).then(() => {
        console.log('Documents imported into Algolia');
        process.exit(0);
    })
    .catch(error => {
        console.error('Error when importing documents into Algolia', error);
        process.exit(1);
    });
})
.catch((err) => {
    console.error('Error getting documents', error);
});
martinseal1987
  • 1,862
  • 8
  • 44
  • 77

1 Answers1

0

This is a Node JS application and here are steps of running it on Windows.

  1. Locate Command Prompt by entering cmd into the search bar. Click cmd in the search results to open the Command Prompt.

    Opening the Command Prompt

  2. Go to the directory where you have your JavaScript files by using the following command.

    cd \path\to\the\directory
    
  3. Type the following command, followed by the name of the application, which is app.js in this case, and then press Enter.

    node app.js
    

    The result of running the application will be printed out to the command prompt.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252