Day 6 – Learning Node JS In 30 Days [Mini series]

Day 6 – Learning Node JS In 30 Days [Mini series]

‘Require’ & ‘Module’

Create two files app.js and song.js. You can create your files as your desired name.

No alt text provided for this image

First we’ll write some codes in song.js file

No alt text provided for this image

I know what you are thinking why there is a extra line module.exports in the end of this file

No alt text provided for this image

This module.exports will available this function sing to everyone who required it. So how to require ?

You can just start require by writing require(‘./song) it will automatically understand it is a js file

Now you can call the sing file by require it and store it in a variable

No alt text provided for this image

Now you can call the function from the app.js file

No alt text provided for this image

You can run the file now by command node app.js

No alt text provided for this image

Final result

No alt text provided for this image

Originally it published on nerdjfpb blog and graphical version on instagram. you can connect with me in twitterlinkedin or instagram


要查看或添加评论,请登录

Muhammad Ali的更多文章

社区洞察

其他会员也浏览了