简体   繁体   English

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

[英]Error: Uncaught ReferenceError: module is not defined when using module.export

Complete error: Uncaught ReferenceError: module is not defined at vsop87Bearth.js:1完整错误:未捕获的 ReferenceError:模块未在 vsop87Bearth.js:1 中定义

I`m trying to use some js files that I found from this( https://github.com/commenthol/astronomia ) repository to calculate the rectangular coordinates of the sun.我正在尝试使用从这个( https://github.com/commenthol/astronomia )存储库中找到的一些 js 文件来计算太阳的直角坐标。 I'm new to js and servers so im not sure on how to use module.exports.我是 js 和服务器的新手,所以我不确定如何使用 module.exports。 There is a file called vsop87Bearth.js with some coordinates that modelates the earth and it looks like this:有一个名为vsop87Bearth.js的文件,其中包含一些模拟地球的坐标,它看起来像这样:

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

And I need to use the vsop87Bearth.js file with a function called position() to do what I need.我需要使用带有名为position()的 function 的vsop87Bearth.js文件来完成我需要的操作。 This is the module Funcion_PSol.js where Im trying to calculate things:这是我试图计算事物的模块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)

Also, the error might be caused from the HTML file, this is it:此外,错误可能是由 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>

Note: I´m using browsersync to host my project and Im not using Node注意:我正在使用 browsersync 来托管我的项目,而我没有使用 Node

Since you are using the esm syntax, you will need to update the export syntax to the following.由于您使用的是 esm 语法,因此您需要将导出语法更新为以下内容。


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

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

What I think you are trying to do is this:我认为您正在尝试做的是:

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

That will import the data you want into the vsop87Bearth variable.这会将您想要的数据导入vsop87Bearth变量。 That variable will then have the properties you want, like earthData.L or earthData.name .然后该变量将具有您想要的属性,例如earthData.LearthData.name

Their README file has more example:https://github.com/commenthol/astronomia#using-a-single-package他们的 README 文件有更多示例:https://github.com/commenthol/astronomia#using-a-single-package


Just a feedback, when you "import as"只是一个反馈,当您“导入为”时

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

You use the earth variable, not data.earth as you had.您使用earth变量,而不是data.earth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM