1

While running my protractor tests , I came up with this scenario, where I had to export a variable from one of my POM file to another. For example, let us suppose file.js is the file where the variable is defined as

file1.js

var xy = some_random_8chars;
 ...
 ...
module.exports={

//other module exports

exportvar1 : xy
}

Now, I need to import this in my file2.js to access the value of xy. So I'm doing this in my file2.js

var ximport = require('file1.js');

var use_new_value = ximport.exportvar1;

console.log(use_new_value) ; 

As per my understanding, this should be printing some_random_8chars. However my console shows undefined, which I am failing to understand. Where I am going wrong?

demouser123
  • 4,108
  • 9
  • 50
  • 82

1 Answers1

-1

Try using this sytax:

export {exportvar1} //in file1

import { exportvar1 } from 'file1'; //in file 2
Benjamin Commet
  • 350
  • 3
  • 8
  • I think this would not work with var. Quoting this from the same link you posted- *that it is not possible to use var, let or const with export default*. – demouser123 May 24 '17 at 12:49
  • Oops you are correct. I think that [this question](https://stackoverflow.com/questions/3922994/share-variables-between-files-in-node-js) may be of more use to you. – Benjamin Commet May 24 '17 at 12:52
  • Yeah. I went through this link, but I'm still getting the undefined error. – demouser123 May 24 '17 at 12:57
  • Is where you are declaring xy out of scope of your export statement? – Benjamin Commet May 24 '17 at 12:57
  • If you try to console.log(xy) right before you export your variables does it print out as undefined? Or does it print the value you want to be in it? – Benjamin Commet May 24 '17 at 16:31
  • it print's out the value of the variable correctly. However, I did the console.log post the export, and then I get an undefined. – demouser123 May 26 '17 at 07:13