【问题标题】:How to install and run Typescript locally in npm?如何在 npm 中本地安装和运行 Typescript?
【发布时间】:2016-10-28 01:23:26
【问题描述】:

我想安装和运行 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 再次安装本地软件包,则会重新引入问题。

【问题讨论】:

    标签: node.js typescript npm


    【解决方案1】:

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

    npm run tsc 
    

    在您传入选项之前包含--(或仅将它们包含在脚本中):

    npm run tsc -- -v
    

    这是一个例子package.json

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

    【讨论】:

    • google 的难题!适用于建议您全局安装但只希望在本地安装的 cli 包,谢谢!
    • 谢谢。,我一直在寻找解决方案,你的答案对我有用。
    • 为什么要在"dependencies"而不是"devDependencies"下安装开发工具?
    【解决方案2】:

    从 npm 5.2.0 开始,一旦您通过本地安装

    npm i typescript --save-dev
    

    ...您不再需要在package.jsonscripts 部分中添加条目——您现在可以使用npx 运行编译器:

    npx tsc
    

    现在您不必每次要使用不同的参数进行编译时都更新您的 package.json 文件。

    【讨论】:

    • 这是一个绝妙的建议。为我节省了大量时间
    • npx tsc 绝对是您需要轻量级、最小配置的本地项目解决方案的最佳选择。它甚至可以在不使用npm i 的情况下用于不经常运行的命令,尽管在不首先运行npm i 的情况下,npx 每次都需要将必要的包安装到临时文件夹中。有关npx 的更多信息,请点击此处:medium.com/@maybekatz/…
    • 这就是我的答案!
    【解决方案3】:

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

    【讨论】:

    • 我在package.json 中所做的有什么不同。所做的只是安装类型脚本并将其保存在我的依赖项中。它也是一个开发依赖(不是普通的依赖)所以它应该是npm install --dev-save
    • @YahyaUddin 你有tsconfig.json 吗?
    • 我尝试了不同的方法来导致您在问题中提供的错误或类似的错误,但我没有成功
    • 这个解决方案现在似乎有效。但是请注意,您不能同时使用filesexclude,根据:github.com/TypeStrong/atom-typescript/blob/master/docs/… 感谢您的帮助。如果可行,我会提供更新!
    • 这就是你没有得到问题的原因。拥有exclude 可以解决问题!
    【解决方案4】:

    您需要告诉 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"
      }
    }
    

    之后,您可以运行任何 TypeScript 命令,例如 npm run tsc -- --init(参数位于第一个 -- 之后)。

    【讨论】:

    • 如果你从你的仓库的根目录(你有你的 node_modules)执行 tsc,这不是必需的,你应该能够在没有额外脚本的情况下运行 tsc。但是当你有一个带有嵌套 package.json 文件的 monorepo 并且想要在那里运行 tsc 而不在 child-repo-directory 中安装 typescript 时,这非常有用!谢谢!
    【解决方案5】:

    您现在可以使用ts-node,让您的生活变得如此简单

    npm install -D ts-node
    npm install -D typescript
    
    ts-node script.ts
    

    【讨论】:

      【解决方案6】:

      tsc 需要配置文件或 .ts(x) 文件才能编译。

      要解决这两个问题,请创建一个名为 tsconfig.json 的文件,其内容如下:

      {
          "compilerOptions": {
              "outFile": "../../built/local/tsc.js"
          },
          "exclude": [
              "node_modules"
          ]
      }
      

      另外,用这个修改你的 npm 运行

      tsc --config /path/to/a/tsconfig.json
      

      【讨论】:

      • 我只需要排除部分就可以了。您也不需要使用 -- config 参数,因为 typescript 会自动在项目的根目录中提供它。
      • 谢谢。我没有意识到这一点
      【解决方案7】:

      请注意,如果您使用的是typings,请执行以下操作:

      rm -r typings
      typings install
      

      如果您在做 angular 2 教程,请使用:

      rm -r typings
      npm run postinstall
      npm start
      

      如果postinstall 命令不起作用,请尝试像这样全局安装类型:

      npm install -g typings
      

      您也可以尝试以下方法,而不是安装后:

      typings install
      

      你应该解决这个问题!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-30
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        • 2019-06-17
        • 2011-09-24
        • 2020-02-28
        • 1970-01-01
        相关资源
        最近更新 更多