Home > OS >  How can I deploy my website to Heroku without including the .env file?
How can I deploy my website to Heroku without including the .env file?

Time:11-21

Hi everyone I am new to heroku and deploying websites online in general is pretty new to me. Everytime I do a heroku push in the cmd line client, I get the error message that says : Error: Cannot find module './../.env' .

I have a .env file that contains my connection string for mongoDB and a secret key. I do not want to push that to the remote server. So I included it in the git.ignore file. I think that is what is causing the issue. It says it cannot find the .env file, I am assuming that is because it is not on git remotely. But how can I deploy the site then without having to push my .env file with information I do not want out to the public?? I do not want to have to push it to git for it to work because that defeats the purpose.

Before I created the .env file I had a config file with a module.exports object and it had the connection string and the secret key in it. But that did not work and I kept getting the same error which was at that time .config module not found. So I looked online and found tutorials that said if I install the dotenv package and use a .env file instead than that should work. But I am getting the exact same error just with the .env file instead. So I see no difference there.

Here are the index.js file and the package.json file. The only thing that is in the .env file is my database connection string and my secret key.

index.js file `


const dotenv = require("dotenv");
dotenv.config({ path: "./.env" });
const { ApolloServer, PubSub } = require("apollo-server");
const mongoose = require("mongoose");

const typeDefs = require("./graphql/typeDefs");
const resolvers = require("./graphql/resolvers");

const pubsub = require("graphql-subscriptions");

const PORT = process.env.port || 5000;

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req }) => ({ req, pubsub }),
});

mongoose
  .connect(process.env.MONGODB_URI, { useNewUrlParser: true })
  .then(() => {
    console.log("MongoDB Connected");
    return server.listen({ port: PORT });
  })
  .then((res) => {
    console.log(`Server running at ${res.url}`);
  })
  .catch((err) => {
    console.error(err);
  });

`

Package.json file `


{
  "name": "social-media-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "serve": "node index",
    "start": "node index"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-server": "^3.10.2",
    "bcryptjs": "^2.4.3",
    "dotenv": "^16.0.3",
    "graphql": "^16.6.0",
    "graphql-subscriptions": "^2.0.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^6.6.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
  
}

`

Here is the error message I keep getting in the client when I push it. I put it in a pastebin because it is quite long.

https://pastebin.com/DC0q27aA

CodePudding user response:

Go to the setting tab. in the Config Vars click on Reveal Config Vars and type your variables

CodePudding user response:

Heroku recommends using the config vars function without saving .env or something to files.

If you save your configuration as a file, be careful because confidential information will likely be disclosed in a public repository.

Please see Heroku's article:https://devcenter.heroku.com/articles/config-vars

  • Related