I've imported dotenv using require('dotenv').config(); at the top of the server.js file.
The .env file is in root folder
require('dotenv').config();
//import dotenv from 'dotenv';
// import express from 'express';
// import viewEngine from '../config/viewEngine';
// import initWebRoute from './routes/web';
// import bodyParser from 'body-parser';
var express = require("express");
var viewEngine = require("./config/viewEngine");
var initWebRoute = require("./routes/web");
var bodyParser = require("body-parser");
let app = express();
// config view engine
viewEngine(app);
//use body-parser to post data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// init all web routes
initWebRoute(app);
let port = process.env.PORT;
app.listen(port, ()=>{
console.log(`App is running at the port ${port}`) ;
});
process.env returns 'undefined'. how can i fix this or is there an alternate way for importing environment file values
PORT=8080
MY_VERIFY_FB_TOKEN=Randomstring
FB_PAGE_TOKEN=pagetoken
Used your snippets and it's working fine. Try running npm i dotenv again to make sure you have the module installed and updated.
btw, body-parser is deprecated. You can use app.use(express.json()); instead.
FIXED the issue. It lacked a .env.example file .env.example works as a guide for creating a .env file with the needed informations that is needs to have the application running.