node.js - 如何在 npm 中本地安装和运行 Typescript?

标签 node.js 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

但是,当我运行第二个命令时,我得到了太多错误,它无法全部显示出来。大部分是这样的:

../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 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 再次安装本地包,它会重新引入问题。

最佳答案

我花了一段时间才弄清楚这个问题的解决方案 - 它在原始问题中。您需要在 package.json 文件中有一个调用 tscscript 以便您可以运行:

npm run tsc 

在你传入选项之前包含 --(或者只是将它们包含在脚本中):

npm run tsc -- -v

这是一个示例 package.json:

{
  "name": "foo",
  "scripts": {
    "tsc": "tsc"
  },
  "dependencies": {
    "typescript": "^1.8.10"
  }
}

关于node.js - 如何在 npm 中本地安装和运行 Typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030078/

相关文章:

javascript - Node.js 模块中的两个 App.js

node.js - 发送实时数据到网络客户端错误捕获

javascript - 在所有全局 javascript 后将 javascript 注入(inject)到 Sailsjs 中的特定 View /路由

javascript - 命名默认参数的 JSDoc Typescript 检查

reactjs - 将绝对路径添加到由 "Create React App"创建的应用程序

python - 为什么django无法正确提供我的SPA静态文件?

node.js - bunyan和winston的优缺点是什么?

javascript - MongoDB Node - 获取插入对象的 _id

javascript - npm 错误!请求 https ://registry. npmjs.org/node-modules 失败,原因:错误:0906D06C:PEM 例程:PEM_read_bio:无起始行

javascript - Typescript - 如果 map 内有条件