Home > Web Front-end > JS Tutorial > body text

Introduction to NodeJS module writing (example code)_javascript skills

WBOY
Release: 2016-05-16 17:55:39
Original
1229 people have browsed it

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 "./".
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template