我在Node js模块上遇到错误?

时间:2018-07-23 12:55:11

标签: node.js

我开始学习node.js。我的模块有问题。我复制并粘贴了来自node.js文档的代码,但仍然出现错误。

module.js文件

/* ----------------------Addition Calculation-------------------- */

const object = require('./lib.js');

console.log(`The sum of 25 and 456 is  + ${object.calculate1(25, 456)}`);

console.log(`The multipication of 25 and 2 is ${object.multiClass(25, 2)}`);

/* --------------------Circle Area Calculation-------------------- */

console.log(`The area of a circle of radius 7 is ${object.area(7)}`);

console.log(`The circumference of a circle of radius 7 is ${object.circumference(7)}`);

/* ----------------------Square Calculation-------------------- */

console.log(`The area of my square is ${object(2).area()}`);

lib.js文件

const { PI } = Math;

exports.calculate1 = (a, b) => a + b;

exports.multiClass = (a, b) => a * b;

exports.area = r => PI * r ** 2;

exports.circumference = r => 2 * PI * r;

exports = (width) => {
    return {
        area: () => width ** 2
    };
};

1 个答案:

答案 0 :(得分:0)

我现在所做的更改

modules.js文件

const object = require('./lib.js');

/* ----------------------Square Calculation-------------------- */

console.log(`The area of my square is ${object(2).area2()}`);

/* ----------------------Addition Calculation-------------------- */

console.log(`The sum of 25 and 456 is ${object.calculate1(25, 456)}`);

console.log(`The multipication of 25 and 2 is ${object.multiClass(25, 2)}`);

/* --------------------Circle Area Calculation-------------------- */

console.log(`The area of a circle of radius 7 is ${object.area(7)}`);

console.log(`The circumference of a circle of radius 7 is ${object.circumference(7)}`);

lib.js文件

const { PI } = Math;

module.exports = width => {
  return {
    area2: () => width ** 2
  };
};

module.exports.calculate1 = (a, b) => a + b;

module.exports.multiClass = (a, b) => a * b;

module.exports.area = r => PI * r ** 2;

module.exports.circumference = r => 2 * PI * r;