認識 node.js
Node.js 是一個獨立的 JavaScript 運行環(huán)境,能獨立執(zhí)行 JS 代碼,可以用來編寫服務器后端的應用程序?;贑hrome V8 引擎封裝,但是沒有 DOM 和 BOM。Node.js 沒有圖形化界面。node -v
檢查是否安裝成功。node index.js
執(zhí)行該文件夾下的 index.js
文件。
modules 模塊化
commonJS 寫法
// a.js
const Upper = (str) => {
return str.substring(0,1).toUpperCase() + str.substring(1)
}
const fn = () => {
console.log("this is a")
}
// 接口暴露方法一:
// module.exports = {
// upper: Upper,
// fn
// }
// 接口暴露方法二:
exports.upper = Upper
exports.fn = fn
// index.js
const A = require('./modules/a')
A.fn() // this is a
console.log(A.upper('hello')) // Hello
ES 寫法文章來源:http://www.zghlxwxcb.cn/news/detail-727137.html
需要先 npm install
安裝依賴,生成 node_modules 文件夾,然后在 package.json 中配置 "type": "module",
,之后才可以使用這種寫法。文章來源地址http://www.zghlxwxcb.cn/news/detail-727137.html
// a.js
const Upper = (str) => {
return str.substring(0,1).toUpperCase() + str.substring(1)
}
const fn = () => {
console.log("this is a")
}
// 接口暴露方法一:
// module.exports = {
// Upper,
// fn
// }
// 接口暴露方法二:
// exports.upper = Upper
// exports.fn = fn
// 接口暴露方法三:
export {
Upper,
fn
}
// index.js
// const fnn = require('./modules/a')
// 注意:此時導入a.js 文件必須加上 js 后綴
import { Upper } from './modules/a.js'
console.log(Upper('hello')) // Hello
到了這里,關(guān)于【Node.js】module 模塊化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!