NodeJs開發高階 發布自定義模組到NPM

2021-10-14 16:02:50 字數 1717 閱讀 3569

注:最好不要用qq郵箱,不然可能會接收不到驗證郵件,實測谷歌郵箱是可以的

module.exports=

, sub:

function

(a, b)

}

,"files":[

"index.js"],

"keywords":[

],"author"

:"my name"

,"license"

:"isc"

}

npm init
npm login
npm publish
注:若是遇到403問題,請先確定是否已經驗證完郵箱,若不是郵箱問題,請參看其他解決方案here

}

npm

install

const module=

require

("modulename"

) console.

log(

'add:'

,module.

add(1,

2))

,"files":[

"dist"],

"devdependencies"

:}

npm

install

,"include":[

"src"],

"exclude":[

"node_modules"

,"dist"

,".idea"

]}

const mod=

require

('./math'

)console.

log(

'add:'

,mod.

add(2,

3))

npm run build
注:這個build就是上面package.json裡scripts裡的build,其實就是執行tsc命令,以將ts檔案轉成目標index.js檔案

node ./dist/index.js

//...

}

npm run build
npm publish
前提是在webstorm裡編寫**

切換到tsc編譯後,可以直接採用export匯出函式或變數了,示例如下

export

var tools =

;export

function

func()

nodejs 自定義模組

1 npm nodejs package manager nodejs包管理器 npm install 安裝模組 npm uninstall 解除安裝模組 npm init 初始化 npm publish 發布自定義模組到npm官網 不過,大牛才用的上吧 o o,繼續努力吧!npm force un...

NodeJS自定義模組

1.建立測試模組js檔案 我這裡命名為test.js 2.新增測試方法 function test console.log test success 3.公開該方法到node模組 這個是public的方法名,外部呼叫的時候,使用這個方法名 exports.test test 4.測試 在另乙個js檔...

NodeJS自定義模組

1.建立測試模組js檔案 我這裡命名為test.js 2.新增測試方法 function test console.log test success 3.公開該方法到node模組 這個是public的方法名,外部呼叫的時候,使用這個方法名 exports.test test 4.測試 在另乙個js檔...