How can I include default model as reference name in node.js?

Viewed 87

I have a module students which I export as a default module

export default students

I need to import this with reference name

I tried: import students as studentModel from "../students";

But it's give me error like studentModel is not definded,

if i user import students from "../students";

then student is work, anyone can guide me what I missed.

Thanks In Advance

2 Answers

You can name the variable, that you're storing the module in, anything you like.

import studentModel from "../students";
// this is just fine

import students from "../students";
// so is this

import dogsGoToHeaven from "../students";
// and this

Option 1

export students;
import {students as studentModel} from "../students";

Exported object will contain property students and when imported it is renamed as studentModel.

Option 2

export default {students};
import {students as studentModel} from "../students";

Exported object will contain property students and when imported it is renamed as studentModel.

Option 3

export default students;
import studentModel from "../students";

Since students itself is exported as default the exported object itself is students. You could directly rename the import to anything you desire.

Related