web-dev-qa-db-ja.com

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

ただし、2番目のコマンドを実行すると、多くのエラーが発生し、すべてを表示できません。そのほとんどは次のようなものです。

../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 ]

パッケージを削除してからTypeScriptをグローバルにインストールすると、問題が解決することに注意してください。ただし、npm installを使用してローカルパッケージを再度インストールすると、問題が再び発生します。

29
Yahya Uddin

プロジェクトにTypeScriptローカルを開発依存関係としてインストールするには、--save-devキーを使用できます

npm install --save-dev TypeScript

また、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

24
Mikhail

この問題の解決策を見つけるのに時間がかかりました-それは元の質問にあります。実行できるようにするには、package.jsonファイルでscriptを呼び出すtscが必要です。

npm run tsc 

オプションを渡す前に--を含めます(または単にスクリプトに含めます)。

npm run tsc -- -v

次にpackage.jsonの例を示します:

{
  "name": "foo",
  "scripts": {
    "tsc": "tsc"
  },
  "dependencies": {
    "TypeScript": "^1.8.10"
  }
}
36
ubershmekel

Npm 5.2.0の時点で、ローカルにインストールしたら

npm i TypeScript --save-dev

... package.jsonscriptsセクションにエントリが不要になりました- npx でコンパイラを実行できるようになりました。

npx tsc

これで、異なる引数でコンパイルするたびにpackage.jsonファイルを更新する必要がなくなりました。

21
Faust

「tsc」がローカルプロジェクトパッケージとして存在することをnpmに伝え(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などのTypeScriptコマンドを実行できます(引数は最初の--の後にあります)。

7
Jim Doyle

tscをコンパイルするには、構成ファイルまたは.ts(x)ファイルが必要です。

両方の問題を解決するには、次の内容のtsconfig.jsonというファイルを作成します。

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

また、これでnpmの実行を変更します

tsc --config /path/to/a/tsconfig.json
2
Bikas

typingsを使用している場合は、次のことに注意してください。

rm -r typings
typings install

angular 2チュートリアルを行う場合、これを使用します。

rm -r typings
npm run postinstall
npm start

postinstallコマンドが機能しない場合は、次のようにタイピングをグローバルにインストールしてみてください。

npm install -g typings

ポストインストールではなく、次を試すこともできます。

typings install

この問題を修正する必要があります!

1
Yahya Uddin