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
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:
Define, and explicitly export,
exports
at the top of SYS/EnvVars.js: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:
and
Though I’m not sure why you need to do this
define
function gimmick, but in case you must, then do this:for consuming:
Reason your existing example was not working is because you were nowhere exporting
envVars
.