我想安装并运行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调试中.我得到的日志(log):

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'tsc' ]
2 info using npm@3.10.2
3 info using node@v5.12.0
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ]
5 info lifecycle foo@1.0.0~pretsc: foo@1.0.0
6 silly lifecycle foo@1.0.0~pretsc: no script for pretsc, continuing
7 info lifecycle foo@1.0.0~tsc: foo@1.0.0
8 verbose lifecycle foo@1.0.0~tsc: unsafe-perm in lifecycle true
9 verbose lifecycle foo@1.0.0~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 foo@1.0.0~tsc: CWD: /home/vagrant/foo
11 silly lifecycle foo@1.0.0~tsc: Args: [ '-c', 'tsc' ]
12 silly lifecycle foo@1.0.0~tsc: Returned: code: 2  signal: null
13 info lifecycle foo@1.0.0~tsc: Failed to exec tsc script
14 verbose stack Error: foo@1.0.0 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 foo@1.0.0
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 foo@1.0.0 tsc: `tsc`
22 error Exit status 2
23 error Failed at the foo@1.0.0 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再次安装本地软件包,它会重新引入问题.

推荐答案

要在项目中安装TypeScript local作为开发依赖项,可以使用--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

Node.js相关问答推荐

无法从Nextjs项目读取环境变量

如何使用多个OR参数从多个集合聚合

Node.js PNG缓冲区获取未知图像格式错误

在Android Studio中react 本机构建失败:未正确检测到Node.js版本

"sh:/usr/local/bin/node:当它存在于文件目录中时未找到

一个函数中的两个依赖的NodeJS数据库操作.如果第二个失败了怎么办?

后端如何保护数据不被用户提取其他用户的数据?

控制台显示一个长对象,我可以';每当我发布更新Mongoose数据库的请求时,我都不知道错误是什么,

使用AWS SDK for JavaScript V3将图像从node.js上传到s3 bucket

如何设置 Puppeteer Select 器的唯一性?

如何使用包含条件正确分页的 sequelize 查询?

如何在没有云功能的情况下使用 Google PubSub

简单的 Node js 程序但执行顺序似乎不同?

[NodeJs 从 ADAL 升级到 MSAL]:无法在字符串上创建属性authenticationScheme

Socket IOFlutter 未连接

Nodejs-console.error vs util.debug

在本地运行 Cloud Functions 会出现错误functions.config() 不可用

编写可维护的事件驱动代码

在 react-router v4 中使用 React IndexRoute

如何断言不为空?