Reference error on node.js while include file

后端 未结 2 1346
离开以前
离开以前 2021-01-19 19:13

I\'m new to node.js. i have two files. they are index.js and db.js

my index.js is

var connection =          


        
相关标签:
2条回答
  • 2021-01-19 19:58

    if you want something from the db module to be accessible in other modules, you need to expose it, by using module.exports

    add to the end of your db.js file:

    module.exports = {
      appModel : appModel,
      Device : Device
    };
    

    and then in your index.js you can do:

    device = new connection.Device({
      id: 93,
      name: 'test1'
    });
    

    you can read more about it here

    0 讨论(0)
  • 2021-01-19 20:08

    node.js treats every file as a module. so to include any source file into another you need to export it at the source end and import it using require at the importing file.

    0 讨论(0)
提交回复
热议问题