skip to Main Content

as shown in the code posted below. i am creating envVars to store all the system constants.in index.js i am importing EnvVars.js as shown below.
at run time, i receive the following error:

PS C:xxxxprojectsnodejshttpprocessTIFF-0> node index.js
file:///C:/xx/xx/projects/nodejs/http/processTIFF-0/index.js:3
import {envVars} from './SYS/EnvVars.js'
        ^^^^^^^
SyntaxError: The requested module './SYS/EnvVars.js' does not provide an export named 'envVars'
?[90m    at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)?[39m
?[90m    at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)?[39m
?[90m    at async Loader.import (internal/modules/esm/loader.js:166:24)?[39m
?[90m    at async Object.loadESM (internal/process/esm_loader.js:68:5)?[39m
PS C:UsersAmr.BakriprojectsnodejshttpprocessTIFF-0>

please let me know how to fix it

EnvVars.js:

export function define(name, value) {
    Object.defineProperty(exports, name, {
        value:      value,
        enumerable: true,
        writable: false
    });
}

define("END_POINT_HOST_JK","sf.tzt")
define("END_POINT_PATH_RASTER_IN_POLYGON","/xx/api/process/polygon-3857?polygon=")
define("END_POINT_PATH_RESOLUTION","&resolution=1")
define("WKT_EPSG4326","POLYGON((14.302759232915124 52.037914242476376,14.301196347357946 12.03581482424235,44.306554812125416 32.03367606317161,24.308149593306208 52.03695283888638,14.302759232915124 52.037914242476376))")

index.js:

import {envVars} from './SYS/EnvVars.js'

2

Answers


  1. You are writing a standard JavaScript module, not a CommonJS module.

    There is no implicit exports variable scoped to the module.

    If you execute SYS/EnvVars.js directly then you would see this error:

    ReferenceError: exports is not defined in ES module scope
    This file is being treated as an ES module because it has a ‘.js’ file extension and ‘/………./package.json’ contains "type": "module". To treat it as a CommonJS script, rename it to use the ‘.cjs’ file extension.

    Define, and explicitly export, exports at the top of SYS/EnvVars.js:

    export const exports = {};
    

    Alternatively, for a more idiomatic approach, get rid of your define function and just export each value individually, creating the object when you import them:

    import * as exports from './SYS/EnvVars.js';
    
    console.log(exports);
    

    and

    export const END_POINT_HOST_JK = 'sf.tzt';
    export const END_POINT_PATH_RASTER_IN_POLYGON = '/xx/api/process/polygon-3857?polygon=';
    export const END_POINT_PATH_RESOLUTION = '&resolution=1';
    export const WKT_EPSG4326 =
        'POLYGON((14.302759232915124 52.037914242476376,14.301196347357946 12.03581482424235,44.306554812125416 32.03367606317161,24.308149593306208 52.03695283888638,14.302759232915124 52.037914242476376))';
    
    Login or Signup to reply.
  2. Though I’m not sure why you need to do this define function gimmick, but in case you must, then do this:

    
    function define(name, value) {
        Object.defineProperty(envVars, name, {
            value:      value,
            enumerable: true,
            writable: false
        });
    }
    
    // this is the actual export
    export let envVars = {};
    
    define("END_POINT_HOST_JK","sf.tzt")
    define("END_POINT_PATH_RASTER_IN_POLYGON","/xx/api/process/polygon-3857?polygon=")
    define("END_POINT_PATH_RESOLUTION","&resolution=1")
    define("WKT_EPSG4326","POLYGON((14.302759232915124 52.037914242476376,14.301196347357946 12.03581482424235,44.306554812125416 32.03367606317161,24.308149593306208 52.03695283888638,14.302759232915124 52.037914242476376))");
    
    

    for consuming:

    
    import {envVars} from './SYS/EnvVars.js'
    
    console.log(envVars.END_POINT_HOST_JK);
    
    

    Reason your existing example was not working is because you were nowhere exporting envVars.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search