node.js 如何在 npm 中本地安装和运行 Typescript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38030078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install and run Typescript locally in npm?
提问by Yahya Uddin
I want to install and run Typescript (i.e. no global dependencies).
我想安装和运行 Typescript(即没有全局依赖项)。
Here is my package.json file:
这是我的 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"
}
I then run:
然后我运行:
npm install
npm run tsc
However when I run the second command I get sooo many errors it cannot display all it. Most of it is like the following:
但是,当我运行第二个命令时,我收到了很多错误,无法显示所有错误。大部分是这样的:
../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'.
In npm-debug.log I get:
在 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 ]
Note that removing the package and then installing typescript globally solves the problem. However if I then use npm install to install the local packages again, it reintroduces the problem.
请注意,删除包然后全局安装打字稿可以解决问题。但是,如果我然后使用 npm install 再次安装本地包,它会重新引入问题。
采纳答案by Mikhail
To install TypeScript local in project as a development dependency you can use --save-devkey
要在项目中本地安装 TypeScript 作为开发依赖项,您可以使用--save-devkey
npm install --save-dev typescript
It's also writes the typescript into your package.json
它还将打字稿写入您的 package.json
You also need to have a tsconfig.jsonfile. For example
您还需要有一个tsconfig.json文件。例如
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
".npm"
]
}
For more information about the tsconfig you can see here http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
有关 tsconfig 的更多信息,您可以在此处查看http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
回答by ubershmekel
It took me a while to figure out the solution to this problem - it's in the original question. You need to have a scriptthat calls tscin your package.jsonfile so that you can run:
我花了一段时间才找出这个问题的解决方案 - 它在原始问题中。您需要在您的文件中有一个script调用tsc,package.json以便您可以运行:
npm run tsc
Include --before you pass in options (or just include them in the script):
--在传入选项之前包含(或仅将它们包含在脚本中):
npm run tsc -- -v
Here's an example package.json:
这是一个例子package.json:
{
"name": "foo",
"scripts": {
"tsc": "tsc"
},
"dependencies": {
"typescript": "^1.8.10"
}
}
回答by Faust
As of npm 5.2.0, once you've installed locally via
从 npm 5.2.0 开始,一旦您通过本地安装
npm i typescript --save-dev
...you no longer need an entry in the scriptssection of package.json-- you can now run the compiler with npx:
...您不再需要-- 您现在可以使用npx运行编译器的scripts部分中的条目:package.json
npx tsc
Now you don't have to update your package.json file every time you want to compile with different arguments.
现在,您不必每次要使用不同参数进行编译时都更新 package.json 文件。
回答by Jim Doyle
You need to tell npm that "tsc" exists as a local project package (via the "scripts" property in your package.json) and then run it via npm run tsc. To do that (at least on Mac) I had to add the path for the actual compiler within the package, like this
您需要告诉 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"
}
}
After that you can run any TypeScript command like npm run tsc -- --init(the arguments come after the first --).
之后,您可以运行任何 TypeScript 命令,例如npm run tsc -- --init(参数在第一个之后--)。
回答by Bikas
tscrequires a config file or .ts(x) files to compile.
tsc需要一个配置文件或 .ts(x) 文件来编译。
To solve both of your issues, create a file called tsconfig.jsonwith the following contents:
要解决这两个问题,请创建一个tsconfig.json包含以下内容的文件:
{
"compilerOptions": {
"outFile": "../../built/local/tsc.js"
},
"exclude": [
"node_modules"
]
}
Also, modify your npm run with this
另外,用这个修改你的 npm run
tsc --config /path/to/a/tsconfig.json
回答by Yahya Uddin
Note if you are using typingsdo the following:
请注意,如果您正在使用,typings请执行以下操作:
rm -r typings
typings install
If your doing the angular 2 tutorial use this:
如果你在做 angular 2 教程,请使用这个:
rm -r typings
npm run postinstall
npm start
if the postinstallcommand dosen't work, try installing typings globally like so:
如果postinstall命令不起作用,请尝试像这样全局安装类型:
npm install -g typings
you can also try the following as opposed to postinstall:
您还可以尝试以下而不是安装后:
typings install
and you should have this issue fixed!
你应该解决这个问题!

