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

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

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:

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

please let me know how to fix it

EnvVars.js:

  1. export function define(name, value) {
  2. Object.defineProperty(exports, name, {
  3. value: value,
  4. enumerable: true,
  5. writable: false
  6. });
  7. }
  8. define("END_POINT_HOST_JK","sf.tzt")
  9. define("END_POINT_PATH_RASTER_IN_POLYGON","/xx/api/process/polygon-3857?polygon=")
  10. define("END_POINT_PATH_RESOLUTION","&resolution=1")
  11. 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:

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

答案1

得分: 1

  1. 虽然我不确定为什么您需要使用这个`define`函数技巧,但如果您必须这样做,那么请执行以下操作:
  2. function define(name, value) {
  3. Object.defineProperty(envVars, name, {
  4. value: value,
  5. enumerable: true,
  6. writable: false
  7. });
  8. }
  9. // 这是实际的导出
  10. export let envVars = {};
  11. define("END_POINT_HOST_JK","sf.tzt")
  12. define("END_POINT_PATH_RASTER_IN_POLYGON","/xx/api/process/polygon-3857?polygon=")
  13. define("END_POINT_PATH_RESOLUTION","&resolution=1")
  14. define("WKT_EPSG4326","POLYGON((14.302759232915124 52.037914242476376,14.301196347357946 12.03581482424235,44.306554812125416 32.03367606317161,24.308149593306208 52.03695283888638,14.302759232915124 52.037914242476376))");

用于使用的代码:

  1. import {envVars} from './SYS/EnvVars.js'
  2. console.log(envVars.END_POINT_HOST_JK);
  1. (Note: I have provided the translation for the code sections, as requested. If you have any further translation needs, please let me know.)
  2. <details>
  3. <summary>英文:</summary>
  4. Though I&#39;m not sure why you need to do this `define` function gimmick, but in case you must, then do this:
  5. ``` lang-js
  6. function define(name, value) {
  7. Object.defineProperty(envVars, name, {
  8. value: value,
  9. enumerable: true,
  10. writable: false
  11. });
  12. }
  13. // this is the actual export
  14. export let envVars = {};
  15. define(&quot;END_POINT_HOST_JK&quot;,&quot;sf.tzt&quot;)
  16. define(&quot;END_POINT_PATH_RASTER_IN_POLYGON&quot;,&quot;/xx/api/process/polygon-3857?polygon=&quot;)
  17. define(&quot;END_POINT_PATH_RESOLUTION&quot;,&quot;&amp;resolution=1&quot;)
  18. 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:

  1. import {envVars} from &#39;./SYS/EnvVars.js&#39;
  2. 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

  1. export const exports = {};

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

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

以及

  1. export const END_POINT_HOST_JK = 'sf.tzt';
  2. export const END_POINT_PATH_RASTER_IN_POLYGON = '/xx/api/process/polygon-3857?polygon=';
  3. export const END_POINT_PATH_RESOLUTION = '&resolution=1';
  4. export const WKT_EPSG4326 =
  5. '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:

  1. 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:

  1. import * as exports from &#39;./SYS/EnvVars.js&#39;;
  2. console.log(exports);

and

  1. export const END_POINT_HOST_JK = &#39;sf.tzt&#39;;
  2. export const END_POINT_PATH_RASTER_IN_POLYGON = &#39;/xx/api/process/polygon-3857?polygon=&#39;;
  3. export const END_POINT_PATH_RESOLUTION = &#39;&amp;resolution=1&#39;;
  4. export const WKT_EPSG4326 =
  5. &#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:

确定