1.0.0 • Published 6 years ago
@kamilmichna/helloworld v1.0.0
HELLO WORLD GCI TASK
node.js module that prints "hello world" created for google code in.
How to use it?
There are two ways to use module:
first - just run the function in main.js!
The main.js file contain:
function printHello(){ console.log('hello world') } module.exports = {printHello}
To see
hello world
in the console you must run the functionprintHello
function printHello(){ console.log('hello world') } module.exports = {printHello}
And voila! We see
hello world
in our console!second way - use it as node module
Our printHello function is in main.js module because of
module.exports = {printHello}
line. So we can easily use it in another file! We can see that is a CJS module - so we can access it in other file by requiring it.
So just create another file , and name it, for example foo.js
. Then as I say, we can access to module by requiring it.
const helloModule = require('./main');
/* Assign module object to the constant, ./main works if our main.js file is in the same directory!*/
helloModule.printHello();
/*Just run the printHello function*/
Then , when we type node foo.js
in terminal , we get hello world
typed in console.
1.0.0
6 years ago