Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

process.env is not defined in angular app #9944

Unanswered
abdallahkadour asked this question in Q&A
Discussion options

My webpack:

const webpack = require('@nativescript/webpack')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const dotenv = require('dotenv')
const packageJson = require('./package.json')

module.exports = (env) => {
  webpack.init(env)
  dotenv.config()
  const isUppercase = (key) => key.toUpperCase() === key
  const envKeys = Object.keys(env)

  let dotEnvValues = envKeys.filter(isUppercase).reduce((memo, key) => {
    return { ...memo, [key]: JSON.stringify(env[key]) }
  }, {})

  webpack.mergeWebpack({
    node: {
      global: true
    },
    resolve: {
      aliasFields: ['browser'],
      alias: {
        process: 'process/browser'
      },
      fallback: {
        stream: require.resolve('stream-browserify'),
        buffer: require.resolve('buffer/'),
        zlib: require.resolve('browserify-zlib'),
        util: require.resolve('util/'),
        fs: false
        // crypto: require.resolve('crypto-browserify')
      }
    }
  })

  //global.process.env = dotEnv
  webpack.chainWebpack((config) => {
    //config.plugin('polyfills').use(NodePolyfillPlugin)
    config.plugin('DefinePlugin').tap((args) => {
      console.log(JSON.stringify(args), args[0])
      Object.assign(args[0], dotEnvValues)
      console.log('the args in define')
      return args
    })
  })

  return webpack.resolveConfig()
}

Any ideas?

You must be logged in to vote

Replies: 1 comment · 2 replies

Comment options

What are you trying to achieve?

If you just want to access values from .env files in your app - you don't really need to do anything special, it's automatically handled by @nativescript/webpack (5.x+)

https://docs.nativescript.org/configuration/webpack#using-dotenv-files

Do note that you cannot do console.log(process.env) or enumerate it, as these values are statically replaced during build time. (Read more in the docs above - there's a note about this).

You must be logged in to vote
2 replies
@alpha-mo
Comment options

why do people keep pointing to removed links at the docs? also I flipped the internet and I still can't make nativescript to read .env files even with a freshly created project

@rigor789
Comment options

Corrected link: https://docs.nativescript.org/configuration/webpack#using-dotenv-files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.