我想安装并运行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
然而,当我运行第二个命令时,我得到了太多的错误,它不能显示所有的错误。大部分是这样的:
../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再次安装本地软件包,它会重新引入问题。
8条答案
按热度按时间jutyujz01#
我花了一段时间才想出这个问题的解决方案-它在原始问题中。你需要在
package.json
文件中有一个调用tsc
的script
,这样你就可以运行:在传入选项之前包含
--
(或者只在脚本中包含它们):下面是一个
package.json
的例子:gcmastyq2#
从npm 5.2.0开始,一旦你通过本地安装了
.你不再需要
package.json
的scripts
部分中的条目--你现在可以用npx运行编译器:现在,您不必在每次使用不同参数进行编译时都更新package.json文件。
bvpmtnay3#
要在项目中安装TypeScript本地作为开发依赖项,您可以使用
--save-dev
键它还将打印脚本写入
package.json
您还需要一个
tsconfig.json
文件。例如有关tsconfig的更多信息,请参见此处http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
wgxvkvu94#
您需要告诉npm“tsc”作为本地项目包存在(通过package.json中的“scripts”属性),然后通过
npm run tsc
运行它。要做到这一点(至少在Mac上),我必须在包中添加实际编译器的路径,如下所示之后,您可以运行任何TypeScript命令,如
npm run tsc -- --init
(参数在第一个--
之后)。20jt8wwn5#
您现在可以使用ts-node,这使您的生活变得简单,
7lrncoxx6#
tsc
需要配置文件或.ts(x)文件来编译。要解决这两个问题,请创建一个名为
tsconfig.json
的文件,其中包含以下内容:另外,用这个修改你的npm运行
brgchamk7#
如果你使用
typings
,请注意:如果你正在使用angular 2教程,请使用这个:
如果
postinstall
命令不起作用,请尝试像这样全局安装类型:您也可以尝试以下操作,而不是postinstall:
你应该有这个问题固定!
gfttwv5a8#
也许这可以帮助:“npx ts-node --esm file.ts”Nodemon命令:nodemon --exec npx ts-node --esm test.ts