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.
Related
I am working on a react/electron project with webpack for bundling and electron-builder to compile the build. I am trying to override the build configuration after its compiled by providing a custom configuration file in the install directory of the application to override the env variables. I have verified process.env updates but the config variables still use the old configuration which was used at the time of compilation. Is there any way this can be achieved?
For example
const apiKey = process.env.APIKEY
In this case process.env.APIKEY has the updated config but apiKey still points to the older key used at the time of compiling the build.
First install dotenv - npm install dotenv
Create an .env file in the same folder as package.json
Then create the environment variables starting with → REACT_APP_
Example of point 3:
Inside .env file → REACT_APP_URL_PATH=xxxxx
Start your project locally and add some console.log(process.env.REACT_APP_URL_PATH)
just to check.
5.- Do the electron thing.
↓↓↓↓↓ npm dotenv ↓↓↓↓↓
https://www.npmjs.com/package/dotenv
I'm wondering how to generate source maps in create-react-app? are they done implicitly? and do they live in the build folder
I've read quite a lot about them being generated with webpack but my app is not using it, so I was wondering how I do this without webpack?
I also don't want to eject
According to CRA documentation, source maps are generated by default in production mode.
However, you can disable this behavior (generating source maps in production mode) by running GENERATE_SOURCEMAP=false ./node_modules/.bin/react-scripts build or if you want this behavior to be permanent, do one of the following solutions:
Set GENERATE_SOURCEMAP=false in your .env file.
Modify the scripts in your package.json and replace "build": "react-scripts build" with "build": "GENERATE_SOURCEMAP=false react-scripts build"
https://facebook.github.io/create-react-app/docs/advanced-configuration
You can truly set GENERATE_SOURCEMAP=false for windows, like #3b3ziz said. However, to run the script across different OS, its better follow the Advanced Configuration Chapter in official document.
Here's what's needed:
Create a .env file in the root of the project. (same folder as your package.json)
Write GENERATE_SOURCEMAP=false in this file.
Rerun whatever you need. react-scripts doesn't detect change in .env.
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.
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.
How do I setup a .npmrc file inside my project where I can define my own private registry? I don't want to have this kind of configuration in my user config .npmrc. Every other developer should be able to just git clone the project and run npm install.
This is what I have so far:
// .npmrc
registry=https://npm.fury.io/AUTH_TOKEN/me/
// package.json:
{
"name": "webapp",
"description": "",
"version": "1.0.0",
"private": true,
"dependencies": {
"jquery": "1.2.3",
"myPrivateLibFromNpmFury": "0.0.4"
}
}
npm install myPrivateLibFromNpmFury
returns
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/myPrivateLibFromNpmFury
As it was pointed out by #Paulpro and #Alexey B. the most parts of it worked already, but I couldn't see it right away, maybe because I didn't reload my bash environment properly. But after that I faced other issue with npm outdated that was caused by the registry url. It turns out npm can only have one registry url, (which is pretty crazy) and if you want to use private and public npm-modules you have to proxy the public npm-module registry through your private registry. Luckily fury.io supports that, so in my case instead of using this:
//.npmrc
registry=https://npm.fury.io/AUTH_TOKEN/me/
i have to use this:
//.npmrc
registry=https://npm-proxy.fury.io/AUTH_TOKEN/USER_NAME/
UPDATE:
It is possible to work around the problem (npm is tied to only one registry). First you have to add a scope to all of your private packages.
Now with .npmrc you can link the registries for the scopes, and you no longer need any proxies at all.
//.npmrc
#project_a:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/
#project_b:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/
#company_a:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/
Noticed to the docs
Per-project config file
When working locally in a project, a .npmrc file in the root of the
project (ie, a sibling of node_modules and package.json) will set
config values specific to this project.
Note that this only applies to the root of the project that you're
running npm in. It has no effect when your module is published. For
example, you can't publish a module that forces itself to install
globally, or in a different location.
I tried to create the files you specified in the question(package.json and .npmrc), everything working fine. Maybe you made a typo somewhere?
frgt$ npm i myPrivateLibFromNpmFury --verbose
npm info using npm#3.3.12
npm info using node#v5.1.1
npm verb request uri https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury
npm verb request no auth needed
npm info attempt registry request try #1 at 14:36:10
npm verb request id 23f09acc4e7021c7
npm http request GET https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury
npm http 403 https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury
You should use the seamless proxy:
registry=https://npm-proxy.fury.io/AUTH_TOKEN/me/
Creating custom npm config for the project
You can create .npmrc file in your project folder, creating .npmrc file override
the default .npmrc which is located in C:\Users\username\.npmrc (it will work as global variable)
# for custom package from your companies artifactory
#myscope:registry=https://mycustomregistry.example.org
# if you want to override with default
registry=https://registry.npmjs.com/