Home > Software design >  Cannot use import statement outside a module calling values from another file
Cannot use import statement outside a module calling values from another file

Time:01-01

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

CodePudding user response:

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;   
});
  • Related