【问题标题】:how to require classes in nodejs如何在nodejs中要求类
【发布时间】:2016-05-14 19:03:01
【问题描述】:

我正在用 Nodejs 编写一个项目,并且我使用的是 ES6。我在Products.js 文件中写了一个名为Products 的类:

class Products
{
...
}

为了将这个类导入另一个文件(如index.js),我使用requre('Products.js'); 并显示此错误:

错误:找不到模块“Products.js”

如何在 index.js 中要求这个类?

【问题讨论】:

  • export default class HelloWorld{}

标签: javascript node.js ecmascript-6


【解决方案1】:

你必须像这样在 require 中包含路径:

var productFile = require("./Products")

考虑 require 语句中的 .(dot)

您还需要像这样导出产品模块 module.exports = Products

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 2018-04-23
    • 2020-07-18
    • 2012-07-25
    • 2018-10-27
    • 2023-03-23
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多