一、Mac上安裝typescript?
sudo npm install -g typescript
測試一下:出現Version則證明安裝成功
tsc -v
二、在VSCode上運行
新建一個xxx.ts文件,測試能否運行
console.log("helloworld")
?運行報錯:ts-node: command not found
再安裝ts-node
sudo npm install -g ts-node
?運行報錯:TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
?在終端上輸入
tsc --init
顯示創建了一個tsconfig.json文件
修改文件
vi tsconfig.json
加上"ts-node": {"esm": true}, 注意不要加到"compilerOptions"里去了
{"ts-node": {"esm": true}, "compilerOptions": {...}
}
再去VSCode里運行,發現成功輸出helloworld?
三、遇到export問題
export enum abc {a = 'aa',b = 'bb'
}
console.log(abc)
運行報錯:ReferenceError: exports is not defined in ES module scope?
?解決方案:在package.json里刪除 "type": "module",
vi package.json
刪掉 "type": "module",?
運行成功