2

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
  • 40,416
  • 14
  • 65
  • 133
Denkhis
  • 165
  • 1
  • 13
  • 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

2

Wrap the variable in your .env file in double quotes (and not single quotes) like this BASE_URL="https://my-url.smth".


I've written an in-depth answer on using env files with nuxt with more information 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
  • 40,416
  • 14
  • 65
  • 133