2016-06-25 54 views
14

我想安裝並運行Typescript(即沒有全局依賴關係)。如何在npm中本地安裝和運行Typescript?

這裏是我的package.json文件:

{ 
    "name": "foo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "tsc": "tsc" 
    }, 
    "devDependencies": { 
    "typescript": "^1.8.10" 
    }, 
    "author": "", 
    "license": "ISC" 
} 

我然後運行:

npm install 
npm run tsc 

然而,當我運行第二個命令,我得到的sooo很多錯誤,它不能顯示全部。大部分是這樣的:

../foo/node_modules/typescript/lib/lib.d.ts(5015,5): error TS2300: Duplicate identifier 'webkitTransformOrigin'. 
../foo/node_modules/typescript/lib/lib.d.ts(5016,5): error TS2300: Duplicate identifier 'webkitTransformStyle'. 
../foo/node_modules/typescript/lib/lib.d.ts(5017,5): error TS2300: Duplicate identifier 'webkitTransition'. 
../foo/node_modules/typescript/lib/lib.d.ts(5018,5): error TS2300: Duplicate identifier 'webkitTransitionDelay'. 
../foo/node_modules/typescript/lib/lib.d.ts(5019,5): error TS2300: Duplicate identifier 'webkitTransitionDuration'. 
../foo/node_modules/typescript/lib/lib.d.ts(5020,5): error TS2300: Duplicate identifier 'webkitTransitionProperty'. 

在NPM-我的debug.log得到:

0 info it worked if it ends with ok 
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'tsc' ] 
2 info using [email protected] 
3 info using [email protected] 
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ] 
5 info lifecycle [email protected]~pretsc: [email protected] 
6 silly lifecycle [email protected]~pretsc: no script for pretsc, continuing 
7 info lifecycle [email protected]~tsc: [email protected] 
8 verbose lifecycle [email protected]~tsc: unsafe-perm in lifecycle true 
9 verbose lifecycle [email protected]~tsc: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/vagrant/foo/node_modules/.bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 
10 verbose lifecycle [email protected]~tsc: CWD: /home/vagrant/foo 
11 silly lifecycle [email protected]~tsc: Args: [ '-c', 'tsc' ] 
12 silly lifecycle [email protected]~tsc: Returned: code: 2 signal: null 
13 info lifecycle [email protected]~tsc: Failed to exec tsc script 
14 verbose stack Error: [email protected] tsc: `tsc` 
14 verbose stack Exit status 2 
14 verbose stack  at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:242:16) 
14 verbose stack  at emitTwo (events.js:100:13) 
14 verbose stack  at EventEmitter.emit (events.js:185:7) 
14 verbose stack  at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14) 
14 verbose stack  at emitTwo (events.js:100:13) 
14 verbose stack  at ChildProcess.emit (events.js:185:7) 
14 verbose stack  at maybeClose (internal/child_process.js:850:16) 
14 verbose stack  at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5) 
15 verbose pkgid [email protected] 
16 verbose cwd /home/vagrant/foo 
17 error Linux 3.13.0-88-generic 
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "tsc" 
19 error node v5.12.0 
20 error npm v3.10.2 
21 error code ELIFECYCLE 
22 error [email protected] tsc: `tsc` 
22 error Exit status 2 
23 error Failed at the [email protected] tsc script 'tsc'. 
23 error Make sure you have the latest version of node.js and npm installed. 
23 error If you do, this is most likely a problem with the foo package, 
23 error not with npm itself. 
23 error Tell the author that this fails on your system: 
23 error  tsc 
23 error You can get information on how to open an issue for this project with: 
23 error  npm bugs foo 
23 error Or if that isn't available, you can get their info via: 
23 error  npm owner ls foo 
23 error There is likely additional logging output above. 
24 verbose exit [ 1, true ] 

注意,刪除軟件包,然後安裝在全球打字稿解決了這個問題。但是,如果我然後使用npm install再次安裝本地軟件包,它會重新引入此問題。

回答

10

安裝打字稿項目地方作爲發展的依賴,你可以使用--save-dev關鍵

npm install --save-dev typescript 

它也寫入打字稿到您的package.json

您還需要有一個tsconfig.json文件。例如

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "system", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false 
    }, 
    "exclude": [ 
    "node_modules", 
    ".npm" 
    ] 
} 

有關tsconfig的更多信息,你可以在這裏看到http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

+0

這與我在'package.json'中做的有什麼不同。所做的只是安裝類型腳本並將其保存在我的依賴項中。這也是一個開發依賴項(不是正常的依賴),所以它應該是'npm install --dev-save' –

+0

@YahyaUddin你有'tsconfig.json'嗎? – Mikhail

+0

沒有。現在補充 –

2

tsc需要一個配置文件或.TS(x)的文件進行編譯。

爲了解決您的兩個問題,創建一個包含以下內容稱爲tsconfig.json文件:

{ 
    "compilerOptions": { 
     "outFile": "../../built/local/tsc.js" 
    }, 
    "exclude": [ 
     "node_modules" 
    ] 
} 

另外,在本

tsc --config /path/to/a/tsconfig.json 
+0

我只需要它的排除部分工作。您也不需要使用' - config'參數,因爲typescript看起來會自動在您的項目的根目錄中提供它。 –

+0

謝謝。我沒有意識到這個 – Bikas

0

注意修改您的NPM運行,如果你正在使用typings做如下:

rm -r typings 
typings install 

如果你在做Ë角2教程使用:

rm -r typings 
npm run postinstall 
npm start 

如果postinstall命令這麼想的工作,嘗試在全球安裝分型,像這樣:

npm install -g typings 

你也可以嘗試以下方法,而不是安裝後:

typings install 

你應該修復這個問題!

12

我花了一段時間才弄清楚這個問題的解決方案 - 這是在原來的問題。你需要有一個script在你package.json文件調用tsc,這樣就可以運行:

npm run tsc 

包括--你在選擇通過之前(或只是包括他們在劇本):

npm run tsc -- -v 

下面是一個例子package.json

{ 
    "name": "foo", 
    "scripts": { 
    "tsc": "tsc" 
    }, 
    "dependencies": { 
    "typescript": "^1.8.10" 
    } 
} 
+1

谷歌的難題!適用於cli軟件包,建議您在全球安裝它,但只希望在本地使用,謝謝! – Cammy

2

你需要告訴NPM說,「TSC」的存在作爲一個本地項目包年齡(通過package.json中的「scripts」屬性),然後通過npm run tsc運行它。要做到這一點(至少在Mac上)我不得不添加的路徑爲包內的實際編譯器,這樣

{ 
    "name": "foo" 
    "scripts": { 
    "tsc": "./node_modules/typescript/bin/tsc" 
    }, 
    "dependencies": { 
    "typescript": "^2.3.3", 
    "typings": "^2.1.1" 
    } 
} 

在此之後,你可以像npm run tsc -- --init運行任何打字稿命令(參數來第一--後)。