2016-06-25 48 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运行任何打字稿命令(参数来第一--后)。