[Vuejs]-What causes the failure to use environment variables in this Vue 3 with TypeScript app?

1πŸ‘

βœ…

I assuming that the @ is understood by your builder as the src folder and the file is found.

The error comes from TypeScript, because you import a .js file and TS does not know what types to expect from it.

I think your best two options are:

  • Rename src/env.js to src/env.ts – then it is a TypeScript file and TS will infer the types automatically
  • If, for some reason, you don’t want to rename the file, add a declaration file (env.d.ts), where you give the types for the module exports manually:
export default {
  api_url: string,
  api_key: string
}

Leave a comment