js :- replacing require() with es6 import



Been working on this for quite some time, finally was able to pen this down. How do you replace 'require(some-library-file)' construct with import.


Say you have older javascript module that looks like this :-


// test.js
function Hello(name) {
console.log('hello' + name)
}

module.exports = Hello;



Then we can import this as shown in code below :-

// main.js
import * as MyHello from './test';
import * as m from 'mongodb';
var greeting = MyHello('test');


The output you will be getting is : hellotest

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm