如何在Nodejs中使用ES6 Module语法导入mathjs模块?

u3r8eeie  于 2023-02-08  发布在  Node.js
关注(0)|答案(2)|浏览(148)

以下是我的工作:

import { zeros } from "mathjs";
const w = zeros(5, 5);

但我想做的是math. zeros(5,5),正如我在文档https://mathjs.org/docs/reference/functions/zeros.html中看到的,它允许我使用math来访问许多函数,如math. matrix()、math. square(array)...等。但当我尝试执行以下操作时:

import { math } from "mathjs";
const w = math.zeros(5, 5);

我收到以下错误

SyntaxError: The requested module 'mathjs' does not provide an export named 'math'

我的package.json看起来像这样:

{
  . . .
  "type": "module",
  . . .
  "dependencies": {
    "mathjs": "^11.5.1"
  }
}
3lxsmp7m

3lxsmp7m1#

参考:MDN:导入

import { zero } from "mathjs"; // it's a name import

像这样导入zero,您只能访问"zero"方法
尝试使用

import math from 'mathjs' // it's a default import of math
const w = math.zeros(5, 5);

通过像这样导入数学,您可以访问它的所有方法

nhaq1z21

nhaq1z212#

尝试用途:

import math from 'mathjs' // it's a default import of math
const w = math.zeros(5, 5);

对我也不起作用。起作用的是以下几点:

import * as math from "mathjs";

如JonrSharpe所建议的。

相关问题