Heroku config vars not accessible in node.js? - javascript

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.

Related

Setting proxy in package.json from .env

I wish to set proxy in package.json from the .env file e.g.
.env:
PROXY="https://test.com:1234"
package.json:
"proxy": $PROXY
or however it should be done.
My reasons for doing so is that I don't want to erroneously check in a package.json file that has a modified proxy to handle my local development conditions.
I'm under the impression using dotenv and cross-var only work for commands not simple string setting.

Not running at local when "npm run serve" command in vue project

I was trying to make some app with vue and installed npm command.
when I run "npm run serve" command, I get the following messages.
It seems that I was supposed to run app at "http://localhost:8080/" and was able to access sample pages, not like "x86_64-apple-darwin13.4.0:" stuff.
is it possible to solve this with changing config file or something ?
App running at:
- Local: http://x86_64-apple-darwin13.4.0:8080/
- Network: http://x86_64-apple-darwin13.4.0:8080/
Note that the development build is not optimized.
To create a production build, run npm run build.
I am supposed to access to http://localhost:8080/ and would get some sample pages.
It seems like your host environment was set to x86_64-apple-darwin13.4.0, you should be able to set the variable in your bash_profile like this
HOST="localhost"
after that reload the environment with source ~/.bash_profile
Looks like you can find an answer to your question here - https://forum.vuejs.org/t/npm-run-serve-is-not-defaulting-to-localhost/88007/13.
In a short:
Add vue.config.js file in a root (same level as package.json file) if you don't have one
Add the following settings to vue.config.js
module.exports = {
devServer: {
host: '127.0.0.1',
port: 8080,
public: 'localhost:8080',
}
}

Load Local Environmental Variables For Use In NPM Scripts

I have an NPM script that looks like this:
"start": "server start --host $DEV_HOST"
There are lots of other scripts that also reference the $DEV_HOST env.
Currently $DEV_HOST is exported in my ~/.bashrc, but I would like to find a way of defining it locally to the project, for example in a .env file.
I wondered if npm offered a preall hook which would allow a simple bash file to be called which could load in the local envs, but no such hook exists.
It also appears that NPM doesn't offer any load mechanism for local envs out of the box.
The only solutions I can currently think of are:
Move all the scripts that need the local env to their own bash files and load in the .env file in each of these.
Call a separate script before each script that needs the local env that loads in the envs from the .env file.
Both of these seem unnecessarily cumbersome and don't scale well.
Is there any way of loading envs defined in a project-local file so that they are available for use in NPM scripts?
Note: I'm not asking how to load envs into an app. I'm asking how to make envs available to npm scripts. The two things are completely different. Dot-env adds envs to the current process. Even if you created a node script that used dot-env to load some envs, those envs wouldn't be available via $ variables as they are not loaded into the environment. They would only be available within the same process via process.env.
You can simply echo an environment variable in your NPM script.
For example, run the following on your command line in your project root:
export SOME_ENV=blalala
then in your package.json you can use it like so:
"scrips:" {
"print-env": "echo $SOME_ENV"
}
this outputs blalala to the console.
If you want to define environment variables in a .env to be available in your scripts use something like this: https://www.npmjs.com/package/better-npm-run
npm i better-npm-run
create your .env file then use the variables exactly as shown above

Automate loading .env when Hubot starts

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.

can not find module express --on windows

I use "npm install -g express" on windows console.but when I try to "node app.js", it shows me the error"can not find module express",I had set the environment variable"NODE_PATH",but nothing happen ,I need your help,Thank you!
Globally installed modules aren't accessible without full path. You need to install express in your project directory or it parents. Check out documentation about module loading.
npm allows two options on how to install a module: locally and globally.
A global installation (done using npm install -g xyz) is for providing some tooling system-wide. Related to express this provides the global express bootstrapper that you can use to create an initial frame for your app by simply typing: express .. If you need help on what you can do with this command, check out its help parameter: express --help.
In contrast, a local installation of a module provides this module for a specific app. A local installation is always made to an app's node_modules folder. When you try to require a module, Node.js searches the this folder for the requested module.
Hence, it is perfectly fine to have express installed multiple times: Once globally for the bootstrapper, multiple times locally (once per app).
So, to cut a long story short: To make your app run, install express locally using npm install express and that's it :-).

Categories

Resources