[FIXED] Calling a variable from a javascript file returns an undefined: Ionic


I’m using the Ionic framework and I want to call a variable from a javascript file, but it returns undefined for the variable and prints the console inside the file.

javascript file:

var test = "Hello";

typescript file:

import * as testfile from "src/assets/js/customers"

export class CustomersPage implements OnInit {
constructor (){
this.test= testfile.test;
console.log('Hello from typescript', this.test);


The Result


Hello from typescript undefined


You should export the variable from your JavaScript file for the value to be accessible inside the TypeScript file.

So inside your "src/assets/js/customers" file it should be

export var test = "Hello";


var test = "Hello";
export test;

If this is not a default export you need to import it like

import * as { testfile } from "src/assets/js/customers"

Answered By – Nitheesh

Answer Checked By – Cary Denson (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published