export NVM_DIR="$HOME/.nvm" && ( git clone https://github.com/creationix/nvm.git "$NVM_DIR" cd "$NVM_DIR" git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" origin` ) && . "$NVM_DIR/nvm.sh"
將以上代碼加入~/.profile文件node
$ nvm install node #node新版已經集成npm工具 $ node -v $ npm -v #經常使用install -g ,install --save |--save-dev , uninstall ,link, test
prefix=/home/someuser/node_global/ #自定義npm模塊安裝目錄 registry=http://registry.npm.taobao.org/ #npm鏡像源
$ npm install typescript -g $ tsc --help
$ npm install ts-node -g $ ts-node
$ npm install typings -g $ typings -h
$ mkdir helloworld && cd helloworld && mkdir src && mkdir src/test $ npm init #生成package.json配置文件 $ typings init #生成typings.json配置文件
{ "filesGlob" :[ "src/**/*.ts" ] , "files": [ "src/index.ts" ,"src/bin/tool.ts" ], "compileOnSave": false, "compilerOptions": { "outDir": "dist/", "moduleResolution": "node", "noImplicitAny": true, "target": "es5" ,"sourceMap": true ,"module": "commonjs" ,"newLine": "LF" } ,"exclude": [ "node_modules" ] }
$ npm link typescript
$ tsc #在dist目錄產生js代碼
$ npm install mocha -g $ npm link mocha $ npm install chai --save $ typings install dt~chai --save #安裝chai聲明文件 $ typings install dt~mocha --save --global#安裝mocha聲明文件
"scripts": { "pretest":"tsc typings/index.d.ts src/test/*.ts --outDir dist" , "test": " mocha dist/test" }
import chai = require('chai'); var expect = chai.expect; describe('User Model Unit Tests:', () => { describe('2 + 4', () => { it('should be 6', (done) => { expect(2+4).to.equals(6); done(); }); it('should not be 7', (done) => { expect(2+4).to.not.equals(7); done(); }); }); });
$ npm test
###結尾 項目代碼git
官方融合typescript2.x+node例子
(https://github.com/Microsoft/TypeScript-Node-Starter)github