Automate loading .env when Hubot starts - javascript

I'm developing an app with the Hubot framework, using Heroku.
I have some config vars set on Heroku, and when I deploy my code, my process.env references work fine.
However, I've had trouble getting my local development system set up with the config vars for testing. I have a .env file and have fetched all the config vars from Heroku. However, the .env file does not seem to be loaded when I start my app at the command line.
I've added hubot-env (as suggested at Hubot - load environmental variables from a file) and can load my .env file manually each time I start my app with the command
hubot env load --filename=[filename]
I'd like to automate this, so this command is automatically executed when I start my bot. Where could I configure this?

Regarding this issue as I understood Hubot doesn't read .env file. Instead of exporting variables each time my solution was to create bash-script run.sh file:
#!/bin/bash
export HUBOT_OWM_APIKEY=MY_OWM_API_KEY;
export HUBOT_WEATHER_UNITS=metric;
HUBOT_SLACK_TOKEN=xoxb-xxxx-MY_SLACK_TOKEN ./bin/hubot --adapter slack
then in bash
$ chmod +x run.sh # provides the permissions
$ ./run.sh # starts the bot with necessary variables

This is a really old question but I'm working on Hubot now and need to save this for posterity.
This is how I'm doing it now. It works without adding additional files or packages:
"scripts": {
"start": "export $(cat .env | xargs) && ./bin/hubot -a slack"
}
Change your adapter from slack to whatever you are using.

Related

How to run Node.js project connected with MongoDB, { downloaded from GitHub } in my pc

I want to know how to run Node.js project connected with MongoDB, { downloaded from GitHub } in my pc
Project link https://github.com/john-smilga/node-express-course/tree/main/06-jobs-api/final
In its read me file it has been written that
#### Project Setup
In order to spin up the project, in the root create .env with these two variables, with your own values.
MONGO_URI
JWT_SECRET
After that run this command
```bash
npm install && npm start
```
I have installed MongoDB community version, but need help in setting up and run this project
I want to run this project in my laptop
As the readme file suggested you first have to create a .env file, then proceed by adding the two required values in your env file:
MONGO_URI=your mongo db URI goes here
JWT_SECRET=your JWT Secret token goes here
then run npm i to install all the required packages and npm start to run the project
This project uses the dotenv
https://github.com/john-smilga/node-express-course/blob/dc0635c55bf27ef77a8aeddf3570892480f2c899/06-jobs-api/final/app.js#L55
You can see that the code reads some variable values from a .env file. This file is not pushed usually for security issues.
create a file named .env at the root of your project. Add two variables:
MONGO_URI = "mongodb://localhost:27017"
JWT_SECRET = <your JWT secret token>
Then run again the server again : npm start

How can I properly connect Realtime Database to a Discord.js bot hosted by heroku

Now I understand that this is a loaded question but every time people make videos showing how to do this, they run the project locally. With heroku I'm lost on where im supposed to find a build pack or something to remove the error saying firebase is not a module.
You need to create a new file on your main folder called .gitignore and you should type
node_modules
.env //If you have these one, you should ignore it too
Clarifications:
Since heroku can deploy your own node_module you need to use .gitignore to hide your node_modules and let the heroku install all of your package for your bot.
If you have .env for your bot token you need to ignore it too, since there's a step to put your token in heroku too.

Deploy Javascript Cron Jobs and Queues on Amazon Elastic Beanstalkd

I have an Amazon Elastic Beanstalk application currently running my NODE.JS app.
I have created some Queues with kue.js and Crons with node-schedule.
Since I have many commands to run the queues and crons, I find it impossible to put it on my current nodejs app.
I am willing to open a new application, the only problem is that I can only run one command.
I really don't want to open a seperate ec2 (not connected to my Elastic Beanstalk service) to run all of those.
What can I do to fix it?
Thank you very much!
As you want to use EB(Elastic Beanstalk) you could write a docker file for the application and EB will already detect that and ask you if this is a docker based project and it will take care of the rest, you just need write all the scripts that you need to run before your Entry point Command CMDnpm start like below
Dockerfile
FROM node:10.13-alpine
# Sets the working directory,and creates the directory as well.
WORKDIR /app
# Install dependencies.
ADD package.json .
RUN npm install
# Copy your source code.
COPY . /app
#Run all your scripts here or simply put them to a scripts.js and run it
RUN node scripts.js
# start app
CMD ["npm", "start"]

Heroku config vars not accessible in node.js?

I have a couple of config vars set up in Heroku: baseURL, NODE_ENV and PROD_MONGODB.
However, trying to access the config var baseURL in my app, it comes up undefined.
Doing console.log(process.env); to see what vars are available gives me this:
{NODE_ENV: "development", PUBLIC_URL: ""}
Why don't some of the vars I set up for Heroku show up or are accessible? Am I missing something obvious?
the problem is that you have set Heroku config vars and it's all good for your production but your local app doesn't have access to those config vars. you need to create another .env file for your local environment vars.
On the root of your project create a folder and call it config
under config folder create a file and name it dev.env
you can set your environment variables here with key=value structure.
add config to your .gitignore file
for example, by setting up PORT=3000 in the dev.env file, you can have access to port 3000 when running your app locally and also have Heroku set its port to whatever it likes to be. Within your app, you only need to use process.env.YOUR_KEY which in this example will be process.env.PORT.
setting up environment vars can be a huge pain as every operating system has it's on way. you can use a npm node module env-cmd to overcome this problem.
first, install it as a development dependency by:
npm i env-cmd --save-dev
now open your package.json file and the blow script to use it on your development:
"scripts": {
"start": "node src/app.js",
"dev": "env-cmd ./config/dev.env nodemon src/index.js"
}
I have assumed that you're using nodemon and you have an index.js file in your src directory.
you can take a look at this answer for Heroku config vars if you need.
https://stackoverflow.com/a/55233621/7274342
Without seeing any of your code, assuming you set the config vars properly, and assuming you are trying to access them via the web and not through your localhost, I would guess that you are attempting to access the config vars client-side. You must bring them in server-side, then distribute them accordingly on the client end.

Getting a MEAN app ready for production

Now that I have completed my MEAN app below are what I think are the stages to get the app ready for production and up and running on Heroku. Could you please advise if I've got the wrong idea as this is my first app of this kind.
1) Use Grunt to lint all Javascript files (front end)
2) Concatenate all the JS files into one file
3) Uglify the concatenated file from step 2
4) Push (dist?) to Heroku (via Git) ... but what do I push?
Will there be files in a "dist" folder at this point?
Is it this directory (and only this directory) that should be pushed to Heroku?
Note: I'm confident with Git and Heroku - I'm not sure what I need to push or indeed what a typical workflow is.
Not sure what you mean by dist, but I can explain how to push to heroku
Make sure you have a package.json with an app name (Heroku will identify it as a node app)
Go to the command line and log in using heroku login Input your details
Create a file and call it Procfile inside the file: web: node app.js
heroku create appName
cd into/your/root/project/folder
'git init'
git add .
git commit -m "commit message"
git push heroku master (Make sure you have heroku as a remote)
There is a way more in depth explanation of this here: https://devcenter.heroku.com/articles/getting-started-with-nodejs#deploy-the-app But they show it with an example app. Don't forget about the Procfile. And about the dist folder, if it's not needed for the app you don't need to add it to the commit
A regular check for the production environment is to run speed tests, seo checks, and such to get the most out of it. You may want to look into canonical links, minify css,javascript,html etc..(serverside minification as well) You can also add domains with heroku which is explained here

Categories

Resources