Using Environment Variables in Angular (ubuntu)

Photo by Markus Spiske on Unsplash
Versions:
Angular CLI: 9.1.1
Node: 12.16.1
OS: linux x64
Angular: 9.1.1
GREETING="hello"
npm i -D @angular-devkit/build-angular @angular-builders/dev-server @angular-builders/custom-webpack dotenv
"options": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
}
"serve": {
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {...
const webpack = require("webpack")
module.exports = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
GREETING: JSON.stringify(process.env.GREETING)
}
})
]
}
node_modules/@types/node/typings.d.ts
declare var process: Process;
interface Process {
env: Env
}
interface Env {
GREETING: string
}
interface GlobalEnvironment {
process: Process
}
const greeting = process.env.GREETING

--

--

Abhinav is a software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store