acoustic85's avatar

loading data from env file in javascript

Hi there im trying to load the files from .env file but it keeps giving me an error

here are the files:

server.js

// server.js

const express = require('express');
const cors = require('cors');
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');

// Load environment variables from .env file
dotenv.config();

const app = express();
app.use(cors());

const client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_SEARCH_ONLY_API_KEY);
const index = client.initIndex('products');

app.get('/search', async (req, res) => {
  const { query } = req.query;

  try {
    const { hits } = await index.search(query);
    res.json(hits);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

appnew.js


require("dotenv").config();

const search = instantsearch({
    appId: process.env.ALGOLIA_APP_ID,
    apiKey: process.env.ALGOLIA_SEARCH_API_KEY,
  indexName:'products',  
  urlSync:true
});

search.addWidget(
    instantsearch.widgets.searchBox({
        container:'#search-input'
    })
);

search.addWidget(
    instantsearch.widgets.hits({
        container:'#hits',
        hitsPerPage:10,
        templates:{
            item:document.getElementById('hit-template').innerHTML,
            empty:"We didn't find any results for the search <em>\"{{query}}\"</em>"
        }
    })
);


search.addWidget(
    instantsearch.widgets.pagination({
        container:'#pagination',
        maxPages: 20,
        
        scrollTo:false
    })
);

search.addWidget(
    instantsearch.widgets.stats({
        container:'#stats-container'
    })
)


search.addWidget(
    instantsearch.widgets.refinementList({
        container:'#refinement-list',
        attributeName:'categories',
        searchable:true,
        
    })
)


search.start();

it keeps breaking in the appnew.js file and its giving me this error Uncaught ReferenceError: require is not defined at appnew.js:1:16

0 likes
0 replies

Please or to participate in this conversation.