最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to require classes in nodejs - Stack Overflow

programmeradmin2浏览0评论

I was writing a project with Nodejs, and I using the ES6. I wrote a class named Products in Products.js file :

class Products
{
...
}

For import this class to another file (like index.js), I use requre('Products.js'); and this error displayed :

Error: Cannot find module 'Products.js'

How to I require this class in index.js?

I was writing a project with Nodejs, and I using the ES6. I wrote a class named Products in Products.js file :

class Products
{
...
}

For import this class to another file (like index.js), I use requre('Products.js'); and this error displayed :

Error: Cannot find module 'Products.js'

How to I require this class in index.js?

Share Improve this question asked May 14, 2016 at 19:03 B. AzizanB. Azizan 932 gold badges4 silver badges13 bronze badges 1
  • 1 export default class HelloWorld{} – The Reason Commented May 14, 2016 at 19:04
Add a ment  | 

1 Answer 1

Reset to default 7

You have to include the path in the require like this :

var productFile = require("./Products")

consider the .(dot) in the require statement

Also you need to export the Products Module like module.exports = Products

发布评论

评论列表(0)

  1. 暂无评论