admin管理员组

文章数量:1022777

Let's say, i have a class instance

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

The problem is that whenever i import user, it just returns an empty object.
Why is this occurring and what should i do in this case?

This is what i'm using for testing

index.js file

const OAuthClient = require('disco-oauth'); //class disco-oauth

const credential = require('./credential.json');

//class instance
const oauthclient = new OAuthClient(credential.id,credential.secret); 

console.log(oauthclient); //Working fine in here

module.exports = oauthclient; //exporting instance  

test.js file

const oauthclient = require('./index')
console.log(oauthclient) //prints {}

Let's say, i have a class instance

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

The problem is that whenever i import user, it just returns an empty object.
Why is this occurring and what should i do in this case?

This is what i'm using for testing

index.js file

const OAuthClient = require('disco-oauth'); //class disco-oauth

const credential = require('./credential.json');

//class instance
const oauthclient = new OAuthClient(credential.id,credential.secret); 

console.log(oauthclient); //Working fine in here

module.exports = oauthclient; //exporting instance  

test.js file

const oauthclient = require('./index')
console.log(oauthclient) //prints {}
Share Improve this question edited Oct 5, 2019 at 10:46 Blayke asked Oct 5, 2019 at 9:46 BlaykeBlayke 1173 silver badges11 bronze badges 6
  • What is User that you are requiring in your file? Also, how are you importing the generated user? – mgarcia Commented Oct 5, 2019 at 10:05
  • This is just an example code, but in reality i'm using disco oauth library . – Blayke Commented Oct 5, 2019 at 10:26
  • Try console.log(user) just bellow const user = new User(id, secret). Does it print what you expect? – Molda Commented Oct 5, 2019 at 10:27
  • const user = require(./user) This is how i'm importing it. @mgarcia – Blayke Commented Oct 5, 2019 at 10:27
  • yes, it is working that way. @mgarcia – Blayke Commented Oct 5, 2019 at 10:32
 |  Show 1 more ment

3 Answers 3

Reset to default 4

you should make file with name User.js and copy this code :

class user {
    constructor(id, secret){
        this.id= id,
        this.secret=secret
    }
 }

 module.exports = user;

and your file is ok for require User class (yourFile.js)

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

and you can make test.js file for import this user (yourFile.js) :

const user = require('./yourFile.js')
console.log(user)

I think the problem is in how you export your class, seems like you used module.export = User instead of module.exports= User

My node server is running in index.js file and i was trying to export the class instance from that file, that's why it didn't work.

Previous code

const http =  require('http'),
bodyparser = require('body-parser'),
OAuthClient = require('disco-oauth');

const app = require('./Api/api');    
const credential = require('./credential.json');

const oauthClient = new OAuthClient(credential.id, credential.secret);
oauthClient.setScopes(['identify', 'guilds', 'connections']);           
oauthClient.setRedirect(credential.redirect);


app.use(bodyparser.urlencoded({extended: false}))
app.use(bodyparser.json())

const server = http.createServer(app),
PORT         = 80;

server.listen(PORT,'0.0.0.0', () => {
    console.log(`running on port ${PORT}`);
})

module.exports = {
    oauthClient
}

I removed OAuthClient from index.js and moved it in separate file and it's working now.

Let's say, i have a class instance

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

The problem is that whenever i import user, it just returns an empty object.
Why is this occurring and what should i do in this case?

This is what i'm using for testing

index.js file

const OAuthClient = require('disco-oauth'); //class disco-oauth

const credential = require('./credential.json');

//class instance
const oauthclient = new OAuthClient(credential.id,credential.secret); 

console.log(oauthclient); //Working fine in here

module.exports = oauthclient; //exporting instance  

test.js file

const oauthclient = require('./index')
console.log(oauthclient) //prints {}

Let's say, i have a class instance

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

The problem is that whenever i import user, it just returns an empty object.
Why is this occurring and what should i do in this case?

This is what i'm using for testing

index.js file

const OAuthClient = require('disco-oauth'); //class disco-oauth

const credential = require('./credential.json');

//class instance
const oauthclient = new OAuthClient(credential.id,credential.secret); 

console.log(oauthclient); //Working fine in here

module.exports = oauthclient; //exporting instance  

test.js file

const oauthclient = require('./index')
console.log(oauthclient) //prints {}
Share Improve this question edited Oct 5, 2019 at 10:46 Blayke asked Oct 5, 2019 at 9:46 BlaykeBlayke 1173 silver badges11 bronze badges 6
  • What is User that you are requiring in your file? Also, how are you importing the generated user? – mgarcia Commented Oct 5, 2019 at 10:05
  • This is just an example code, but in reality i'm using disco oauth library . – Blayke Commented Oct 5, 2019 at 10:26
  • Try console.log(user) just bellow const user = new User(id, secret). Does it print what you expect? – Molda Commented Oct 5, 2019 at 10:27
  • const user = require(./user) This is how i'm importing it. @mgarcia – Blayke Commented Oct 5, 2019 at 10:27
  • yes, it is working that way. @mgarcia – Blayke Commented Oct 5, 2019 at 10:32
 |  Show 1 more ment

3 Answers 3

Reset to default 4

you should make file with name User.js and copy this code :

class user {
    constructor(id, secret){
        this.id= id,
        this.secret=secret
    }
 }

 module.exports = user;

and your file is ok for require User class (yourFile.js)

const User = require(./User);

const id = '123456',
secret   = '8787';

const user = new User(id, secret)

module.exports = user;

and you can make test.js file for import this user (yourFile.js) :

const user = require('./yourFile.js')
console.log(user)

I think the problem is in how you export your class, seems like you used module.export = User instead of module.exports= User

My node server is running in index.js file and i was trying to export the class instance from that file, that's why it didn't work.

Previous code

const http =  require('http'),
bodyparser = require('body-parser'),
OAuthClient = require('disco-oauth');

const app = require('./Api/api');    
const credential = require('./credential.json');

const oauthClient = new OAuthClient(credential.id, credential.secret);
oauthClient.setScopes(['identify', 'guilds', 'connections']);           
oauthClient.setRedirect(credential.redirect);


app.use(bodyparser.urlencoded({extended: false}))
app.use(bodyparser.json())

const server = http.createServer(app),
PORT         = 80;

server.listen(PORT,'0.0.0.0', () => {
    console.log(`running on port ${PORT}`);
})

module.exports = {
    oauthClient
}

I removed OAuthClient from index.js and moved it in separate file and it's working now.

本文标签: javascriptHow to export class instance in NodeStack Overflow