Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt dotenv-module and deploying to Netlify #25

Closed
pburdette opened this issue Jan 3, 2019 · 11 comments
Closed

Nuxt dotenv-module and deploying to Netlify #25

pburdette opened this issue Jan 3, 2019 · 11 comments

Comments

@pburdette
Copy link

Version

1.3.0

Reproduction link

https://github.com/beardedpayton/paytonburdette.com-v2

Steps to reproduce

Install module

npm i nuxtjs/dotenv

** cant include the @ symbol here, it thinks I'm trying to tag a user in the issue

Require module in nuxt.config.js

require('dotenv').config()

Add module to modules array in nuxt.config.js

modules: ["@nuxtjs/dotenv"]

Use "module in codebase with process.env.ENV_VAR_NAME

What is expected ?

Works as expected locally. I can read from my .env file fine.

When deploying to Netlify and using build environment variables, I should be able to read from those variables within my project.

What is actually happening?

I cannot read variables from my build environment variables established with Netlify. I can only read from them if a .env file is present.

This bug report is available on Nuxt community (#c20)
@ghost ghost added the cmty:bug-report label Jan 3, 2019
@yann-yinn
Copy link

yann-yinn commented Jan 8, 2019

My previous answer was incorrect ( I deleted it) and probably does not fix your issue.

You probably want to set your env property in nuxt.config.js, for example:

module.exports = {
  env: {
    LAMBDA_FUNCTIONS_BASE_URL: process.env.LAMBDA_FUNCTIONS_BASE_URL,
    CONTACT_FORM_TO: process.env.CONTACT_FORM_TO
  },
  // ...

In your component, you can now use them :

      sendMail({
        to: process.env.CONTACT_FORM_TO,
        email: this.inputs.email,
        message: this.inputs.message
      })

Your .env file won't be available on Netlify server as it is not versioned by default (see Nuxt .gitignore file). But Netlify environment variables are accessible simply with process.env.YOUR_VARIABLE.

On your local machine, the code above will still use the variables from your .env file. (thanks to @nuxtjs/dotenv module)

Note that process.env.YOUR_VARIABLE is normally only accessible from server side. That's why we need to use the env property from Nuxt config file , so that process.env.XXX can be replaced with the right values on client side too.

@nuxtjs/dotenv does this automatically from .env file, but as we do not have .env file on Netlify server, it can not help us on Netlify.

@nuxtjs/dotenv is not a way to access environment variables, but a way to create environment variables from you .env file

@mornir
Copy link

mornir commented Jan 27, 2019

@yann-yinn Whoa! Thank you! I've spent quite some time figuring out why I was getting errors when navigating to new pages on my website. The first page rendered on the server was fine, but then the process.env variables were undefined.

@nuxtjs/dotenv does this automatically from .env file, but as we do not have .env file on Netlify server, it can help us on Netlify.

I don't fully understand this sentence. Did you mean nuxtjs/dotenv cannot help us on Netlify?

So did I understand this correctly: when the page is rendered on the server, Netlify is able to replace the process.env variables. But when the web-app turned into a SPA after the first navigation, Netlify have "no more control". That is why we have Netlify store the process.env variables in the nuxt.config.js file, so that they can be accessed throughout the web-app?

@yann-yinn
Copy link

yann-yinn commented Jan 27, 2019

@mornir sorry I made a typo, i should have wrote "it can not help us on Netlify". I corrected the sentence on the above post.

You got it :) process.env is only for the server. This is not supposed to work at all on client side; but Nuxt provides us a way to make it work on client side by using the env property of nuxt.config.js file. ( using webpack to replace variables from client code at compile time i guess)

@kissu
Copy link

kissu commented Sep 18, 2019

I guess I missed that one in the docs, thanks guys ! 🍉 💚

@ricardogobbosouza
Copy link
Member

@JulienTant can closed?

@AllanOricil
Copy link

AllanOricil commented Apr 27, 2020

It is not working in one of my projects. It is very particular to this project and I don't know what Im doing wrong. I did not deploy my .env file as always. I have also configured properly all the env variables in netlify.
And locally it works as expected. Dotenv reads my local .env and configures my env variables.

To test the problem

Open the console and refresh the page. I have printed 3 ENV Variables that were available and configured in Netlify during the build.
https://cranky-mayer-c2e711.netlify.app/login/
The first 3 logs are using a Computed Property. The other 3 are using a console.log(process.env.ENV_NAME). I printed this way because I thought the problem was the computed property, but it seems webpack is not changing my env properties, since a simple console.log prints undefined.
image

image

Locally I can see that Webpack exposed the variables to my client

//DEV
image

//BUILD
image

Netlify ENV Variables

image

This is my project dependencies

 "dependencies": {
    "@nuxtjs/auth": "^4.9.1",
    "@nuxtjs/axios": "^5.3.6",
    "@nuxtjs/moment": "^1.6.0",
    "@nuxtjs/pwa": "^3.0.0-0",
    "bootstrap-vue": "^2.12.0",
    "js-yaml": "^3.13.1",
    "nuxt": "^2.12.2",
    "randomstring": "^1.1.5",
    "vue-mermaid": "0.0.12",
    "vuex-persist": "^2.2.0"
  },
  "devDependencies": {
    "@nuxtjs/dotenv": "^1.4.1",
    "@nuxtjs/eslint-config": "^2.0.0",
    "@nuxtjs/eslint-module": "^1.0.0",
    "@vue/test-utils": "^1.0.0-beta.27",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.1.0",
    "eslint": "^6.1.0",
    "eslint-plugin-nuxt": ">=0.4.2",
    "jest": "^24.1.0",
    "vue-jest": "^4.0.0-0"
  }

This is my nuxt.config

/* eslint-disable no-unused-vars */
/* eslint-disable nuxt/no-cjs-in-config */
/* eslint-disable object-shorthand */
const path = require('path')
const fs = require('fs')

const env = process.env.NODE_ENV

const envPath = path.resolve(process.cwd(), '.env')
if (env !== 'production' && envPath && fs.existsSync(envPath)) {
  require('dotenv').config({
    path: envPath
  })
}

console.log('NECESSARY ENV ENVIRONMENTS')
console.log(process.env.GHCI)
console.log(process.env.GHCS)
console.log(process.env.GHRU)

module.exports = {
  mode: 'spa',
  server: {
    port: 3000
  },
  env: {
      GHCI: process.env.GHCI || '19c1497ec754bc87217e',
      GHCS: process.env.GHCS || '86be62ff07d9f829898a4a4e8ddc9684a198f285',
      GHRU: process.env.GHRU || 'http://localhost:3000/workflows'
  },
  /*
  ** Headers of the page
  */
  head: {
    title: process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href:
          'https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css'
      }
    ],
    scripts: [
      {
        src: 'https://code.jquery.com/jquery-3.2.1.slim.min.js',
        integrity: 'sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN'
      },
      {
        src: 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js',
        integrity: 'sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q'
      },
      {
        src: 'https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js',
        integrity: 'sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl'
      }
    ]
  },
  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },
  /*
  ** Global CSS
  */
  css: [
    '~/assets/css/main.css',
    '~/assets/css/dark.css',
    '~/assets/css/light.css'
  ],
  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    { src: '~plugins/vue-mermaid.js', ssr: false },
    { src: '~/plugins/vuex-persist', ssr: false }
  ],
  /*
  ** Nuxt.js dev-modules
  */
  buildModules: [
    '@nuxtjs/eslint-module',
    '@nuxtjs/moment',
    '@nuxtjs/dotenv'
  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/pwa',
    'bootstrap-vue/nuxt'
  ],

  dotenv: {
    systemvars: true
  },
  /*
  ** Axios module configuration
  ** See https://axios.nuxtjs.org/options
  */
  axios: {
    proxy: true,
    browserBaseURL: process.env.BASE_URL,
    retry: false,
    progress: false
  },

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    analyse: true,
    extend (config, ctx) {
    }
  }
}

Nuxt and Webpack versions

image

Netlify Build Log. In this image you can see clearly that the env variables were available.

image

Netlify Full Build Log
6:36:22 PM: Build ready to start
6:36:24 PM: build-image version: 2dbd444fcdce00cf06325060a8238d5ae3e86774
6:36:24 PM: build-image tag: v3.3.7
6:36:24 PM: buildbot version: ed44b74dccd74a30dec84aacc6e49a3511498577
6:36:24 PM: Fetching cached dependencies
6:36:25 PM: Failed to fetch cache, continuing with build
6:36:25 PM: Starting to prepare the repo for build
6:36:25 PM: No cached dependencies found. Cloning fresh repo
6:36:25 PM: git clone https://github.com/AllanOricil/github-actions-board
6:36:26 PM: Preparing Git Reference refs/heads/master
6:36:27 PM: Starting build script
6:36:27 PM: Installing dependencies
6:36:28 PM: Downloading and installing node v10.20.1...
6:36:28 PM: Downloading https://nodejs.org/dist/v10.20.1/node-v10.20.1-linux-x64.tar.xz...
6:36:28 PM: 
#######################
6:36:28 PM:                                        32.7%
6:36:28 PM: 
############################################
6:36:28 PM: ############################ 100.0%
6:36:28 PM: Computing checksum with sha256sum
6:36:28 PM: Checksums matched!
6:36:31 PM: Now using node v10.20.1 (npm v6.14.4)
6:36:31 PM: Attempting ruby version 2.6.2, read from environment
6:36:32 PM: Using ruby version 2.6.2
6:36:32 PM: Using PHP version 5.6
6:36:32 PM: Started restoring cached node modules
6:36:32 PM: Finished restoring cached node modules
6:36:33 PM: Installing NPM modules using NPM version 6.14.4
6:36:56 PM: > core-js@2.6.11 postinstall /opt/build/repo/node_modules/core-js
6:36:56 PM: > node -e "try{require('./postinstall')}catch(e){}"
6:36:56 PM: Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
6:36:56 PM: The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
6:36:56 PM: > https://opencollective.com/core-js 
6:36:56 PM: > https://www.patreon.com/zloirock 
6:36:56 PM: Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
6:36:56 PM: > ejs@2.7.4 postinstall /opt/build/repo/node_modules/ejs
6:36:56 PM: > node ./postinstall.js
6:36:56 PM: Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)
6:36:56 PM: > bootstrap-vue@2.13.0 postinstall /opt/build/repo/node_modules/bootstrap-vue
6:36:56 PM: > opencollective || exit 0
6:36:57 PM:                           ;iiiiiiiiiiSSSSSSSSiiiiiiiiii;
6:36:57 PM:                           .rXXXXXXXXXrrrrrrrSXXXXXXXXXr.
6:36:57 PM:                           :iXXXXXXXX2. ;;;;, r3XXXXXXXi;
6:36:57 PM:                          ,rSSSSSXXXX2..sSSi: r3XXXSSSSSr,
6:36:57 PM:                           ,siiiiS2XX2. :;;:,.rXX2Siiiis,
6:36:57 PM:                            ,riiiii2X2..5XXXi .22iiiiir,
6:36:57 PM:                             .riiiii22..::::,,r2iiiiir.
6:36:57 PM:                              .riiiii5SSiiiiS22iiiiir.
6:36:57 PM:                                ;iiiii5X3333X5iiiii;
6:36:57 PM:                                 :iiiiiSXXXXSiiiii:
6:36:57 PM:                                  :siiiiSXXSiiiis:
6:36:57 PM:                                   ,siiiiiiiiiis,
6:36:57 PM:                                    .riiiiiiiir.
6:36:57 PM:                                     .riiiiiir.
6:36:57 PM:                                      .;iiii;.
6:36:57 PM:                                        ;ii;
6:36:57 PM:                                         ::
6:36:57 PM:                        Thanks for installing bootstrap-vue
6:36:57 PM:                  Please consider donating to our open collective
6:36:57 PM:                         to help us maintain this package.
6:36:57 PM:                            Number of contributors: 257
6:36:57 PM:                               Number of backers: 122
6:36:57 PM:                               Annual budget: $6,181
6:36:57 PM:                              Current balance: $3,164
6:36:57 PM:               Donate: https://opencollective.com/bootstrap-vue/donate
6:36:57 PM: > nuxt@2.12.2 postinstall /opt/build/repo/node_modules/nuxt
6:36:57 PM: > opencollective || exit 0
6:36:58 PM:                                      :-:
6:36:58 PM:                                    .==-+:
6:36:58 PM:                                   .==. :+- .-=-
6:36:58 PM:                                  .==.   :==++-+=.
6:36:58 PM:                                 :==.     -**: :+=.
6:36:58 PM:                                :+-      :*+++. .++.
6:36:58 PM:                               :+-      -*= .++: .=+.
6:36:58 PM:                              -+:      =*-   .+*: .=+:
6:36:58 PM:                             -+:     .=*-     .=*-  =+:
6:36:58 PM:                           .==:     .+*:        -*-  -+-
6:36:58 PM:                          .=+:.....:+*-.........:=*=..=*-
6:36:58 PM:                          .-=------=++============++====:
6:36:58 PM:                           Thanks for installing nuxtjs
6:36:58 PM:                  Please consider donating to our open collective
6:36:58 PM:                         to help us maintain this package.
6:36:58 PM:                            Number of contributors: 229
6:36:58 PM:                               Number of backers: 321
6:36:58 PM:                               Annual budget: $75,821
6:36:58 PM:                              Current balance: $26,466
6:36:58 PM:                  Donate: https://opencollective.com/nuxtjs/donate
6:37:00 PM: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules/fsevents):
6:37:00 PM: npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
6:37:00 PM: npm WARN
6:37:00 PM: optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.3 (node_modules/chokidar/node_modules/fsevents):
6:37:00 PM: npm WARN notsup
6:37:00 PM:  SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
6:37:00 PM: added 1663 packages from 802 contributors and audited 924560 packages in 26.153s
6:37:01 PM: 51 packages are looking for funding
6:37:01 PM:   run `npm fund` for details
6:37:01 PM: found 0 vulnerabilities
6:37:01 PM: NPM modules installed
6:37:01 PM: Started restoring cached go cache
6:37:01 PM: Finished restoring cached go cache
6:37:01 PM: unset GOOS;
6:37:01 PM: unset GOARCH;
6:37:01 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.12.linux.amd64';
6:37:01 PM: export PATH="/opt/buildhome/.gimme/versions/go1.12.linux.amd64/bin:${PATH}";
6:37:01 PM: go version >&2;
6:37:01 PM: export GIMME_ENV='/opt/buildhome/.gimme/env/go1.12.linux.amd64.env';
6:37:01 PM: go version go1.12 linux/amd64
6:37:01 PM: Installing missing commands
6:37:01 PM: Verify run directory
6:37:01 PM: Executing user command: npm run build
6:37:02 PM: > github-actions-board@1.0.0 build /opt/build/repo
6:37:02 PM: > nuxt build
6:37:02 PM: NECESSARY ENV ENVIRONMENTS
6:37:02 PM: 58958856329f69bdce86
6:37:02 PM: af2ca4cf9563ac44cfa88255b1e53726ee864bf8
6:37:02 PM: http://localhost:5000/workflows
6:37:02 PM:  WARN  No .env file found in /opt/build/repo.
6:37:04 PM: ℹ Production build
6:37:04 PM: ✔ Builder initialized
6:37:04 PM: ✔ Nuxt files generated
6:37:05 PM: ℹ Compiling Client
6:37:48 PM: ✔ Client: Compiled successfully in 43.13s
6:37:48 PM: Hash: d7fc3b6d25c31919aacf
6:37:48 PM: Version: webpack 4.43.0
6:37:48 PM: Time: 43134ms
6:37:48 PM: Built at: 04/27/2020 9:37:48 PM
6:37:48 PM:                          Asset        Size  Chunks                                Chunk Names
6:37:48 PM: ../server/client.manifest.json    66.3 KiB          [emitted]
6:37:48 PM:        1fc75f5dd344eeda4312.js   311 bytes       2  [emitted] [immutable]         pages/index
6:37:48 PM:        3941ac35a6e9e1d28362.js   361 bytes       4  [emitted] [immutable]         pages/login
6:37:48 PM:        6b16e3fcd642bc472bb5.js    2.43 KiB       6  [emitted] [immutable]         runtime
6:37:48 PM:        7d659c40cf1f49588530.js     173 KiB       8  [emitted] [immutable]         vendors.pages/jobs/_id
6:37:48 PM:        84ae3c566f90a8a4e3be.js     1.8 MiB       7  [emitted] [immutable]  [big]  vendors.app
6:37:48 PM:        8c342721bbe7fffb1bd6.js    8.35 KiB       3  [emitted] [immutable]         pages/jobs/_id
6:37:48 PM:                       LICENSES    3.99 KiB          [emitted]
6:37:48 PM:        a78f77e0854880378b74.js    88.5 KiB       0  [emitted] [immutable]         app
6:37:48 PM:        d176ecac666f8648d6e2.js     185 KiB       1  [emitted] [immutable]         commons.app
6:37:48 PM:        ef308df56b550f263084.js     4.3 KiB       9  [emitted] [immutable]
6:37:48 PM:        fd2f404f54bad0390cc9.js     7.6 KiB       5  [emitted] [immutable]         pages/workflows/index
6:37:48 PM:              fonts/41e8dea.ttf     141 KiB          [emitted]
6:37:48 PM:      icons/icon_120.5f6a36.png    4.68 KiB          [emitted]
6:37:48 PM:      icons/icon_144.5f6a36.png     5.8 KiB          [emitted]
6:37:48 PM:      icons/icon_152.5f6a36.png     6.1 KiB          [emitted]
6:37:48 PM:      icons/icon_192.5f6a36.png    7.83 KiB          [emitted]
6:37:48 PM:      icons/icon_384.5f6a36.png    18.1 KiB          [emitted]
6:37:48 PM:      icons/icon_512.5f6a36.png      20 KiB          [emitted]
6:37:48 PM:       icons/icon_64.5f6a36.png    2.35 KiB          [emitted]
6:37:48 PM:                img/11d42ee.svg    1.02 KiB          [emitted]
6:37:48 PM:                img/283c671.svg    2.45 KiB          [emitted]
6:37:48 PM:                img/9428d16.svg  1020 bytes          [emitted]
6:37:48 PM:                img/9a45759.svg    2.05 KiB          [emitted]
6:37:48 PM:                img/a172fa1.svg    1.74 KiB          [emitted]
6:37:48 PM:                img/a5644b0.svg  1020 bytes          [emitted]
6:37:48 PM:                img/aa83c82.svg    1.08 KiB          [emitted]
6:37:48 PM:                img/b9d22a0.svg    2.09 KiB          [emitted]
6:37:48 PM:         manifest.22805a31.json   796 bytes          [emitted]
6:37:48 PM:  + 1 hidden asset
6:37:48 PM: Entrypoint app [big] = 6b16e3fcd642bc472bb5.js d176ecac666f8648d6e2.js 84ae3c566f90a8a4e3be.js a78f77e0854880378b74.js
6:37:48 PM: WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
6:37:48 PM: This can impact web performance.
6:37:48 PM: Assets:
6:37:48 PM:   84ae3c566f90a8a4e3be.js (1.8 MiB)
6:37:48 PM: WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (1000 KiB). This can impact web performance.
6:37:48 PM: Entrypoints:
6:37:48 PM:   app (2.07 MiB)
6:37:48 PM:       6b16e3fcd642bc472bb5.js
6:37:48 PM:       d176ecac666f8648d6e2.js
6:37:48 PM:       84ae3c566f90a8a4e3be.js
6:37:48 PM:       a78f77e0854880378b74.js
6:37:48 PM: 
6:37:48 PM: ℹ Generating pages
6:37:48 PM: ✔ Generated /login
6:37:48 PM: ✔ Generated /workflows
6:37:48 PM: ✔ Generated /
6:37:48 PM: Skipping functions preparation step: no functions directory set
6:37:48 PM: Caching artifacts
6:37:48 PM: Started saving node modules
6:37:48 PM: Finished saving node modules
6:37:48 PM: Started saving pip cache
6:37:48 PM: Finished saving pip cache
6:37:48 PM: Started saving emacs cask dependencies
6:37:48 PM: Finished saving emacs cask dependencies
6:37:48 PM: Started saving maven dependencies
6:37:48 PM: Finished saving maven dependencies
6:37:48 PM: Started saving boot dependencies
6:37:48 PM: Finished saving boot dependencies
6:37:48 PM: Started saving go dependencies
6:37:48 PM: Finished saving go dependencies
6:37:52 PM: Build script success
6:37:52 PM: Starting to deploy site from 'dist'
6:37:52 PM: Creating deploy tree 
6:37:53 PM: Creating deploy upload records
6:37:53 PM: 2 new files to upload
6:37:53 PM: 0 new functions to upload
6:37:53 PM: Starting post processing
6:37:54 PM: Post processing done
6:37:54 PM: Site is live
6:38:22 PM: Finished processing build request in 1m58.074404544s

@AllanOricil
Copy link

AllanOricil commented Apr 28, 2020

I Just did a test that points to a Netlify bug.
I built locally using my env variables, started the to app locally and the vars were there. Then I dragged and dropped the dist folder to Netlify. It accepted the files and showed me that the site was ready. I cleared all my browser data, removed the Application Cache clicking in the button Clear Cache, disabled the service worker and refreshed the page. The site was the same. They are not using the one I built locally and the builds that their server does are not working too.
Dragging the dist folder to create a New site fixed the issue with Netlify.
Did anybody had this problem before?

@deeja
Copy link

deeja commented May 24, 2020

@AllanOricil Netlify can't really have a delivery JS bug because it's not executing anything.

  1. Build script runs - all code is compiled or generated on this step. I prefer nuxt generate but nuxt build works too.
  2. Files are copied to a static file server. There is no process at this point, just a static website.

I've just written a blog post on how the Nuxt js system passes environment variables. You can check that out if you want https://dev.to/deeja/nuxt-js-environment-variables-without-dotenv-4oj8

@AllanOricil
Copy link

@AllanOricil Netlify can't really have a delivery JS bug because it's not executing anything.

  1. Build script runs - all code is compiled or generated on this step. I prefer nuxt generate but nuxt build works too.
  2. Files are copied to a static file server. There is no process at this point, just a static website.

I've just written a blog post on how the Nuxt js system passes environment variables. You can check that out if you want https://dev.to/deeja/nuxt-js-environment-variables-without-dotenv-4oj8

Hi @deeja I discovered that Netlify is caching some files to deploy it faster. When I created a new project on Netlify, and deployed the same code, without changing anything, the new environment variables were there.

Copy link
Member

atinux commented Dec 16, 2020

Please try again but using the Nuxt runtimeConfig: https://nuxtjs.org/blog/moving-from-nuxtjs-dotenv-to-runtime-config#migrating-to-the-nuxtjs-runtime-config-from-nuxtjsdotenv

@manuelgeek
Copy link

My previous answer was incorrect ( I deleted it) and probably does not fix your issue.

You probably want to set your env property in nuxt.config.js, for example:

module.exports = {
  env: {
    LAMBDA_FUNCTIONS_BASE_URL: process.env.LAMBDA_FUNCTIONS_BASE_URL,
    CONTACT_FORM_TO: process.env.CONTACT_FORM_TO
  },
  // ...

In your component, you can now use them :

      sendMail({
        to: process.env.CONTACT_FORM_TO,
        email: this.inputs.email,
        message: this.inputs.message
      })

Your .env file won't be available on Netlify server as it is not versioned by default (see Nuxt .gitignore file). But Netlify environment variables are accessible simply with process.env.YOUR_VARIABLE.

On your local machine, the code above will still use the variables from your .env file. (thanks to @nuxtjs/dotenv module)

Note that process.env.YOUR_VARIABLE is normally only accessible from server side. That's why we need to use the env property from Nuxt config file , so that process.env.XXX can be replaced with the right values on client side too.

@nuxtjs/dotenv does this automatically from .env file, but as we do not have .env file on Netlify server, it can not help us on Netlify.

@nuxtjs/dotenv is not a way to access environment variables, but a way to create environment variables from you .env file

this is a savior. day closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants