-1

Unable to Resolve the Import issue, already have type = module added in package.json file

import { env, username, password, panel } from "../DemoAutomationProject/config";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

[![testcase.jsfile][1]][1]

configjs file

Unable to solve this import error

dependencies

prit
  • 41
  • 8

1 Answers1

1

You are doing wrong while you are importing baseConfig.You have access to the function only but not the local variables to the function. You need to return an object which you consume later.

async function baseConfig() {
 ///
}

module.export;


import { env, username, password, panel } from "../DemoAutomationProject/config";

Solution: you need to import the function baseConfig. It will returns you the object having all the data.

    async function baseConfig() {
     return {
       env: '',
       username: '',
       password: '',
       panel: '',
    }

module.export = baseConfig;

Now in import do this.

const config = require('../../DemoAutomationProject/config');
config().then(data => {
 const { env, username, password, panel } = data;   
});
deepak
  • 1,390
  • 1
  • 8
  • 12
  • `return { ^^^^^^ SyntaxError: Unexpected token 'return'` getting this error @deepak and if call return `async function baseConfig() { { env: '', username: '', password: '', panel: '', } module.export; – prit Dec 28 '21 at 17:44
  • 1
    Oh , I have updated the code. Async function return promise so we need to use then to get the data. – deepak Dec 29 '21 at 05:45
  • `TypeError: conf is not a function` still getting this error with below code @deepak `const conf = require('../DemoAutomationProject/config'); conf().then(data => { const { env, username, password } = data; });` – prit Dec 29 '21 at 07:52
  • My Requirement is to Keep the Config information "config.js" in one File and Used these base values in all the other files testcase1.js and testcase2.js like wise – prit Dec 29 '21 at 08:05
  • 1
    check module.export. it should be module.export= baseConfig – deepak Dec 29 '21 at 08:47
  • 1
    why it is async function. It should be a normal function as you are hardcoding the value. In that case it will be normal (remove async from baseConfig). const { env, username, password, panel } = config(); – deepak Dec 29 '21 at 08:48
  • Thanks deepak didnt needed `config().then(data => { const { env, username, password, panel } = data; }); ` btw what is this statement for ? - > This works `module.export = baseConfig;` in base config file and `const config = require('../DemoAutomationProject/config.js');` in main file Thanks @deepak – prit Dec 31 '21 at 14:15
  • you need to tell what you are going to export from this file. – deepak Jan 03 '22 at 05:25