nodejs安装与使用

下载地址

node.js官网下载地址

安装

#正常流程安装即可
#安装node会自带npm

工具安装

#地址 visual studio 写js的工具
https://code.visualstudio.com/docs/?dv=win
#安装完成之后 安装code running 即可运行js

安装插件

vscode-icons
vscode-fileheader
HTML CSS Supportss

模块化思想

#两种写法 推荐第一种
//写法一
function hello() {
    console.log('Hello, world!');
}

function greet(name) {
    console.log('Hello, ' + name + '!');
}

module.exports = {
    hello: hello,
    greet: greet
};
//写法二
function hello() {
    console.log('Hello, world!');
}

function greet(name) {
    console.log('Hello, ' + name + '!');
}

function hello() {
    console.log('Hello, world!');
}
exports.hello = hello;
exports.greet = greet;

实例

hello.js

'use strict';

var s = 'Hello';

function greet(name) {
    console.log(s + ', ' + name + '!');
}

function hi(name) {
    console.log('Hi, ' + name + '!');
}

function goodbye(name) {
    console.log('Goodbye, ' + name + '!');
}

module.exports = {
    greet: greet,
    hi: hi,
    goodbye: goodbye
};

main.js

'use strict';

const hello = require('./hello');
var s = 'Michael';
hello.greet(s);
hello.goodbye(s);

指令使用

#创建package.json包
node init 
#封装并形成一个tgz压缩包
npm pack
#创建用户(https://npmjs.org/signup 上注册账户,方便将模块发布)
npm adduser
#发布模块
npm publish
#安装模块
npm install <项目名称>
#删除模块 ,特殊情况下使用--force强制删除
npm uninstall <项目名称> 
#引入模块,加不加.js后缀都可以
require("cindy) or require("./lib/utils.js")

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/83475251