请求的模块未提供导出名称。

huangapple go评论64阅读模式
英文:

the requested module does not provide export name

问题

以下是翻译好的部分:

"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:\xx\xx\projects\nodejs\http\processTIFF-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:\Users\Amr.Bakri\projects\nodejs\http\processTIFF-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'"

英文:

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:\xx\xx\projects\nodejs\http\processTIFF-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:\Users\Amr.Bakri\projects\nodejs\http\processTIFF-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'

答案1

得分: 1

虽然我不确定为什么您需要使用这个`define`函数技巧,但如果您必须这样做,那么请执行以下操作:

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

// 这是实际的导出
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))");

用于使用的代码:

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

console.log(envVars.END_POINT_HOST_JK);

(Note: I have provided the translation for the code sections, as requested. If you have any further translation needs, please let me know.)

<details>
<summary>英文:</summary>

Though I&#39;m not sure why you need to do this `define` function gimmick, but in case you must, then do this:

``` lang-js

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

// this is the actual export
export let envVars = {};

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

for consuming:


import {envVars} from &#39;./SYS/EnvVars.js&#39;

console.log(envVars.END_POINT_HOST_JK);

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

答案2

得分: 0

以下是翻译好的部分:

你正在编写一个标准的JavaScript模块,而不是CommonJS模块

模块中没有隐式的exports变量。

如果直接执行SYS/EnvVars.js,你会看到以下错误:

ReferenceError: exports在ES模块范围内未定义
之所以将此文件视为ES模块,是因为它具有'.js'文件扩展名,并且'/........../package.json'包含"type": "module"。要将其视为CommonJS脚本,请将其重命名为使用'.cjs'文件扩展名。

SYS/EnvVars.js的顶部定义并显式导出exports

export const exports = {};

或者,采用更符合惯例的方法,摒弃定义函数,只需在导入它们时单独导出每个值,并在导入时创建对象:

import * as exports from './SYS/EnvVars.js';

console.log(exports);

以及

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))';
英文:

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<br>
> 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 &#39;./SYS/EnvVars.js&#39;;

console.log(exports);

and

export const END_POINT_HOST_JK = &#39;sf.tzt&#39;;
export const END_POINT_PATH_RASTER_IN_POLYGON = &#39;/xx/api/process/polygon-3857?polygon=&#39;;
export const END_POINT_PATH_RESOLUTION = &#39;&amp;resolution=1&#39;;
export const WKT_EPSG4326 =
	&#39;POLYGON((14.302759232915124 52.037914242476376,14.301196347357946 12.03581482424235,44.306554812125416 32.03367606317161,24.308149593306208 52.03695283888638,14.302759232915124 52.037914242476376))&#39;;

huangapple
  • 本文由 发表于 2023年6月26日 16:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定