how to import modules that require arguments

shangsunset

this is my files sturcture:

-models
    -user.js
    -room.js
    -database.js
-controllers
    -createRoom.js
    -routes.js
    ..

user.js and room.js are modules i want to import in database.js. right now im doing like this:

in database.js:

var mysql = require('mysql');
var Sequelize = require('sequelize');
var db = new Sequelize('test', 'root', 'root', {
dialect: "mysql", 
port: 3306

})
var User = require('./user.js')(Sequelize, db);
var Room = require('./room.js')(Sequelize, db);


module.exports = function(){

    //code...   

};

in user.js/room.js

module.exports = function (Sequelize, db) { 

var Room = db.define('Room', {
    room_id : {type: Sequelize.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true},
})

db
 .sync({force: true})
 .complete(function (err) {})

so far so good, but when i need to import user.js or room.js in other files besides database.js(eg, createRoom.js), i have trouble importing them because theres no Sequelize and db defined like in the database.js. does it mean i have to connect to the database and require Sequelize again whenever i need to use user.js and room.js module in other files? is there a better way to work around this?? thanks!

greelgorke

you have different possibilities here

  1. re-export models:

    in your database.js

    //init code as seen
    module.exports.Room = Room
    

    importing somewhere else

    require('../models/database').Room
    
  2. make your database.js dependency

    in database.js

     module.export = { Sequelize: Sequelize, db : db}
    

    in room.js

     var dbModule = require('./database')
     module.exports = dbModule.Sequelize.define('Room', /*....*/)
    
  3. Attach the model to the init function

    in room.js

    module.exports = roomInit
    
    function roomInit(Sequelize, db) { 
    
      roomInit.Room = db.define('Room', {
        room_id : {type: Sequelize.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true}
    })
    
     db
       .sync({force: true})
       .complete(function (err) {})
     }
    

    usage

     require('./room').Room
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using require() and import for private npm modules failing

cannot import or require node modules (using npm)

How do I change these require statements for these modules to use import statements instead?

How can Include both commonJs require syntax and es modules import syntax in the same bundle - webpack

How can I import a Typescript 1.0 function like a node.js require with arguments?

How to require CommonJS modules in the browser?

How to import modules that import other modules without using import *

How to transform the require to import mode?

Can't import npm modules in commonjs with rollup : "require is not defined"

import css file from node_modules using require()

Require, Import, Require Import

How to import modules in IPython Clusters

How to import tested modules in tests?

how to import scripts as modules in ipyhon?

Web Workers - How To Import Modules

How to import zig modules dynamically?

how to import async modules with JS

How to require deep nested NodeJS modules?

How to require modules from another module - Nodejs

requireJS and node modules. How to require?

How arguments are handled in Require JS define function

Cannot import npm modules after converting ES5 require to ES6 import

How to use import instead of require with node js?

How to create an express Application with import instead of require

How to rewrite this require module with import module syntax

How to use 'require' to import a JSON in NestJS controller?

how to export function so it is accessible with import and require?

Nodejs require()(require()) to import

How to import modules from a folder outside node modules in next js