错误:未捕获ReferenceError:使用module.export时未定义模块

时间:2020-05-14 02:33:06

标签: javascript html module.exports

完全错误:未捕获的ReferenceError:在vsop87Bearth.js:1中未定义模块

我正在尝试使用从{https://github.com/commenthol/astronomia)存储库中找到的一些js文件来计算太阳的直角坐标。我是js和服务器的新手,所以我不确定如何使用module.exports。 有一个名为 vsop87Bearth.js 的文件,其中包含一些模拟地球的坐标,看起来像这样:

module.exports = {
  stuff: numbers,
  name: "earth"
};

我需要将 vsop87Bearth.js 文件与名为 position()的函数一起使用,以执行所需的操作。这是模块 Funcion_PSol.js ,我在其中尝试计算内容:

import position from './astronomia-master/src/solarxyz.js'
import Planet from './astronomia-master/src/planetposition.js'
import * as earth from './astronomia-master/data/vsop87Bearth.js' //I'm not sure of THIS line
var tierra = new Planet(earth);
var pos = position(earth, 2448908.5)

此外,该错误可能是由HTML文件引起的,就是这样:

<!DOCTYPE html>
<html>
<head>
    <script type="module" src="./astronomia-master/data/vsop87Bearth.js"></script>
    <script type="module" src="Funcion_PSol.js"></script>
</head>
</html>

注意:我正在使用browsersync托管我的项目,而我没有使用Node

2 个答案:

答案 0 :(得分:1)

由于您使用的是 esm 语法,因此您需要将导出语法更新为以下内容。


export = {
  stuff: numbers,
  name: "earth"
};

https://nodejs.org/api/esm.html#esm_json_modules

答案 1 :(得分:0)

我认为您正在尝试做的是这样:

const earthData = require('astronomia/data/vsop87Bearth')

这会将您想要的数据导入到vsop87Bearth变量中。然后,该变量将具有所需的属性,例如earthData.LearthData.name

他们的自述文件有更多示例:https://github.com/commenthol/astronomia#using-a-single-package


当您“导入为”时只是一个反馈

import * as earth from './astronomia-master/data/vsop87Bearth.js'

您使用earth变量,而不是像以前那样使用data.earth