Home >Web Front-end >JS Tutorial >Introduction to NodeJS module writing (example code)_javascript skills

Introduction to NodeJS module writing (example code)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:55:391262browse

We know that each module corresponds to a js file. This article writes the simplest module hello.js, and then requires the customized module in another js file (main.js).

hello.js

Copy code The code is as follows:

function hello( name) {
console.log('hello, ' name);
}
exports.hello = hello;


main.js
Copy code The code is as follows:

var h = require('./hello');
h. hello('snandy');

Convention: hello.js and main.js are in the same directory, such as the node directory
Open the command line, enter the node directory, and execute the command
Copy code The code is as follows:

node main.js


You can see the command line output: hello, snandy

Note:
The require parameter in main.js cannot be "hello" and must be preceded by "./".
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn