Monday, 17 April 2017

What's right way to import a module javascript node.js

I'm a little confused about how to import a module in my node project. I see two ways that are working, but what is the right way; or is there any any difference? I am referring to the case where the module I'm importing is at least one directory up.

Way #1:

const myModule = require('../myModule');

Way #2:

const myModule = require('./../myModule');

Also, I see some imports are done using the file extension, and other don't. What's the correct way?

const myModule = require('./myModule');

or

const myModule = require('./myModule.js');

Any feedback will be greatly appreciated.



via jac0117

No comments:

Post a Comment