node.js - Typescript'i npm'de yerel olarak nasıl kurar ve çalıştırırsınız?


Typescript'i kurmak ve çalıştırmak istiyorum (yani global bağımlılıklar olmadan).

İşte benim package.json dosyam:

{
  "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"
}

Sonra şunu çalıştırırım:

npm install
npm run tsc

Ancak ikinci komutu çalıştırdığımda o kadar çok hata alıyorum ki hepsini gösteremiyor. Çoğu aşağıdaki gibi:

../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 dosyasında şunu alıyorum:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'tsc' ]
2 info using [email 
3 info using [email 
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ]
5 info lifecycle [email ~pretsc: [email 
6 silly lifecycle [email ~pretsc: no script for pretsc, continuing
7 info lifecycle [email ~tsc: [email 
8 verbose lifecycle [email ~tsc: unsafe-perm in lifecycle true
9 verbose lifecycle [email ~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 ~tsc: CWD: /home/vagrant/foo
11 silly lifecycle [email ~tsc: Args: [ '-c', 'tsc' ]
12 silly lifecycle [email ~tsc: Returned: code: 2  signal: null
13 info lifecycle [email ~tsc: Failed to exec tsc script
14 verbose stack Error: [email  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 
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  tsc: `tsc`
22 error Exit status 2
23 error Failed at the [email  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 ]

Paketi kaldırıp ardından typescript'i küresel olarak yüklemenin sorunu çözdüğünü unutmayın. Ancak daha sonra yerel paketleri tekrar yüklemek için npm install kullanırsam, sorun yeniden ortaya çıkıyor.

Cevap #1

Bu sorunun çözümünü bulmam biraz zaman aldı - bu, orijinal soruda var. Şunu çalıştırabilmek için package.json dosyanızda tsc çağıran bir script olmalı:

npm run tsc 

Seçenekleri geçmeden önce -- ekleyin (veya bunları doğrudan script'e dahil edin):

npm run tsc -- -v

İşte bir örnek package.json:

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

Cevap #2

npm 5.2.0 itibarıyla artık package.json dosyasındaki scripts bölümünde bir girişe ihtiyacınız yok. Artık derleyiciyi npx ile çalıştırabilirsiniz:

npx tsc

...ve farklı argümanlarla derleme yapmak istediğinizde her seferinde package.json dosyasını yeni bir script ile güncellemeniz gerekmez.

Cevap #3

TypeScript'i projede geliştirme bağımlılığı olarak yerel olarak yüklemek için --save-dev anahtarını kullanabilirsiniz

npm install --save-dev typescript

Ayrıca TypeScript'i package.json dosyanıza yazar

Ayrıca bir tsconfig.json dosyanızın olması da gerekir. Örneğin

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

Tsconfig hakkında daha fazla bilgi için buraya bakabilirsiniz http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Cevap #4

npm'e "tsc"nin yerel bir proje paketi olarak var olduğunu (package.json dosyanızdaki "scripts" özelliği aracılığıyla) bildirmeniz ve ardından npm run tsc ile çalıştırmanız gerekiyor. Bunu yapmak için (en azından Mac'te) paketin içindeki gerçek derleyici için yolu eklemem gerekti, şöyle ki

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

Bundan sonra, npm run tsc -- --init gibi herhangi bir TypeScript komutunu çalıştırabilirsiniz (argümanlar ilk --'den sonra gelir).

Cevap #5

Artık ts-node kullanabilirsiniz, bu da hayatınızı şu kadar basit hale getirir

npm install -D ts-node
npm install -D typescript

ts-node script.ts

İlgili makaleler