2017-02-16 22 views
2

編輯#1:好像我有一個工作配置,所有的改進建議都是受歡迎的。見回答:https://stackoverflow.com/a/42269408/1155847在package.json中配置本地打字機編譯器


原題

我目前正在試圖建立我的環境,讓我package.jsondevDependencies打字稿版本將被使用。什麼是這方面的一些最佳實踐,所以它是「編輯不知情」,最好可以用作npm腳本,例如:npm run tscompile

要清楚 - 當使用npm install typescript -g時,我可以使所有的工作都能正常工作 - 但後來我依賴於全球安裝的版本,這不是我想要的 - 因爲我們希望在團隊中工作並設置在升級之前爲每個成員提供特定的打字稿版本,所以我們都在同一頁面上。

我目前正在嘗試將它設置爲這樣 - 然後npm然後抱怨它不能識別「node_modules」作爲內部或外部命令......我認爲我必須將tsconfig.json傳遞給tsc或者至少給它「工作目錄」 - 但我甚至無法從我本地下載的npm緩存中啓動tsc。

的package.json

{ 
    "name": "tswithnodejsgettingstarted", 
    "version": "1.0.0", 
    "description": "", 
    "main": "app/index.js", 
    "scripts": { 
    "start": "node app/index.js", 
    "tscompile": "node_modules/typescript/tsc" 
    }, 
    "author": "", 
    "license": "ISC", 
    "devDependencies": { 
    "typescript": "2.1.6" 
    } 
} 

tsconfig.json

{ 
    "compileOnSave": true, 
    "compilerOptions": { 
     "module": "commonjs", 
     "noImplicitAny": true, 
     "sourceMap": true, 
     "outDir": "app" 
    }, 
    "include": [ 
     "src/**/*.ts" 
    ], 
    "exclude": [ 
     "node_modules" 
    ] 
} 

回答

1

確定..現在看來似乎是這麼簡單(見下文)。在這裏回答,爲其他人尋找答案。或者請讓我知道如果有更好的解決方案

package.json內配置腳本,如"tsc": "tsc"。然後運行npm run tsc,它會使用你在本地安裝的tsc版本,並發現你的tsconfig.json ofcourse。它不使用你的全球版本 - 因爲我卸載了那個 - 只是在命令行出錯時輸入tsc

例如爲:

檢查repo *我在那裏玩這個。

的package.json

{ 
    "name": "tswithnodejsgettingstarted", 
    "version": "1.0.0", 
    "description": "", 
    "main": "app/index.js", 
    "scripts": { 
    "start": "npm run tsc && node app/index.js", 
    "tsc": "tsc" 
    }, 
    "author": "", 
    "license": "ISC", 
    "devDependencies": { 
    "typescript": "2.1.6" 
    } 
} 

*回購:https://github.com/pluralsight-courses/typescript-fundamentals/tree/master/001-GettingStarted