4

I'm trying to build and deploy my angular 9 project in production environment. The main goal is to protect my back-end services IP address and credentials as these environments can't be exposed to anyone for security issue. Build and Serve works fine for current setups but the problem is i can't access/read environment variables except one that i created using shell. Only i can read the value of TEST1 variable but BASE_URL and NOTIFICATION_URL shows undefined. One important thing is that i have no idea about system OS environment variable and don't system environment will work in production build. I just googled and created those variables in shell and echo shows value properly but i'm not sure i'm in a proper way. Please Suggest me the suitable way or currently where i made mistake. And what about production?

Shell variables i created:

shell variables

Custom Webpack

const webpack = require('webpack');

module.exports = {
    plugins: [new webpack.DefinePlugin({
        'process.env': {
            BASE_URL: JSON.stringify(process.env.BASE_URL),
            NOTIFICATION_URL: JSON.stringify(process.env.NOTIFICATION_URL),
            TEST1: JSON.stringify(process.env.TEST1)
        }
    })]
}

typings.d.ts

// @ts-ignore
declare var process: Process;

interface Process {
    env: Env
}

interface Env {
    BASE_URL: string
    NOTIFICATION_URL: string
    TEST1: string
}

interface GlobalEnvironment{
    process: Process;
}

environment.prod.ts

export const environment = {
    production: true,
    baseUrl: `${process.env.BASE_URL}`,
    notificationUrl: `${process.env.NOTIFICATION_URL}`,
    test: `${process.env.TEST1}`,
};
1
  • every service provides you as a local variable environment to create/delete variables instead of creating in the terminal. if you're using Heroku devcenter.heroku.com/articles/config-vars, or you just can create .env file using ssh! Commented Jul 25, 2020 at 6:23

1 Answer 1

0

TO SET environment variables permanently on server

edit /etc/environment

Paste:

BASE_URL=http://localhost:8080/
TEST_URL=http://localhost:8080/

Save

To Apply on current run: source /etc/environment

Note: When you ssh into the machine and use export TEST=test, Then a session wide environment variable would be created, Only the commands in that session can access it. And it would get deleted after closing the ssh session.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.