Search code examples
node.jscucumberwebdriver-io

Unable to import from a relative path using node js


Currently my automation framework built on Cucumber+ Nodejs+ webdriverio. It has the following structure for data files

    main/
    ..../data
    ......../region1.js
    ......../region2.js

In my step definition I need to import the data files so that my functions can use the data as per the region I intend to execute which I provide during run time

How should I mention my import command? for example, I tried the following but that does not work import users from '../main/data/*';


Solution

  • Posting a solution which I came across

    Step 1: Add an index.js file under /data folder Step 2: Adding the following code in index.js

        import * as region1 from "../region1"
        import * as region2 from "../region2"
    
        export {
         region1,
         region2
        }
    

    Now in the file that requires this data, add the following import line

        import myValues from "./main/data"
    

    You need to pass your required region from command line as an environment variable lets say as REGION

    If you want to access a value from respective region file based on value passed in REGION, the following code will work

    const myreqData = myValues[process.env.REGION].<respective node in the js]