1

I'm trying to add environment variable inside .env file in my nuxt project.

My Nuxt.js version is 2.15.3

Here is a snippet from my nuxt.config.js:

export default {
  publicRuntimeConfig: {
    baseURL: process.env.BASE_URL
  },
  ssr: false,
  target: 'static',
}

Here is my .env file:

BASE_URL=https://my-url.smth

But when I run npm run dev then nuxt gives error:

ERROR in ./.env 1:14 Module parse failed: Unexpected token (1:14) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.

I was guided by this article https://nuxtjs.org/tutorials/moving-from-nuxtjs-dotenv-to-runtime-config/

Also it DOES NOT work with the old way through the @nuxtjs/dotenv.

kissu
  • 24,311
  • 11
  • 36
  • 67
Denkhis
  • 75
  • 8
  • Could you please show your entire `nuxt.config.js` where related to the env? also, are you using `nuxt-vite`? – DengSihan Nov 25 '21 at 14:42
  • 1
    Also, maybe try to write it as `BASE_URL="https://my-url.smth"` The issue here, being a "loader", it is probably not directly a matter of env variables but more of a structure one or the way that your project is setup. Do you have a `package.json` to share? :) – kissu Nov 25 '21 at 16:51

1 Answers1

1

EDIT: the solution was to wrap the variable into double quotes like this BASE_URL="https://my-url.smth".


I've wrote an in-depth answer on the subject.
As mentioned in it, please do not use the @nuxtjs/dotenv package.

If you still have an issue, we'll probably need more details or at least a repro to help you more efficiently.

kissu
  • 24,311
  • 11
  • 36
  • 67