【问题标题】:Configure local typescript compiler inside package.json在 package.json 中配置本地 typescript 编译器
【发布时间】:2017-07-05 06:57:23
【问题描述】:

编辑 #1:似乎我有一个工作配置,但欢迎所有改进它的建议。见答案:https://stackoverflow.com/a/42269408/1155847


原始问题

我目前正在尝试设置我的环境,以便使用我的package.jsondevDependencies 打字稿版本。有哪些最佳实践,使其“编辑不知道”,最好可以用作 npm 脚本,例如:npm run tscompile

需要明确 - 使用 npm install typescript -g 时我可以让一切正常工作 - 但是我依赖于全球安装的版本,这不是我想要的 - 因为我们想要在一个团队中工作并开始升级前每个成员都有一个特定的打字稿版本,所以我们都在同一个页面上。

我目前正在尝试像这样设置它 - 但是 npm 然后抱怨它不将“node_modules”识别为内部或外部命令......我想我必须将 tsconfig.json 传递给tsc 也是如此,或者至少给它“工作目录”——但我什至无法从本地下载的 npm 缓存中启动 tsc。

package.json

{
  "name": "tswithnodejsgettingstarted",
  "version": "1.0.0",
  "description": "",
  "main": "app/index.js",
  "scripts": {
    "start": "node app/index.js",
    "tscompile": "node_modules/typescript/tsc"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "2.1.6"
  }
}

tsconfig.json

{
    "compileOnSave": true,
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "sourceMap": true,
        "outDir": "app"
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

【问题讨论】:

    标签: node.js typescript npm visual-studio-code tsconfig


    【解决方案1】:

    好的...看起来就这么简单(见下文)。在这里回答它,以供其他寻找答案的人使用。或者请告诉我是否有更好的解决方案

    package.json 中配置像"tsc": "tsc" 这样的脚本。然后只需运行npm run tsc,它将使用您在本地安装的 tsc 版本,当然会发现您的 tsconfig.json。它不使用您的全局版本 - 因为我卸载了那个 - 只是在命令行错误中输入 tsc

    例如:

    检查我正在玩这个的repo*。

    package.json

    {
      "name": "tscnodejsgettingstarted",
      "version": "1.0.0",
      "description": "",
      "main": "app/index.js",
      "scripts": {
        "start": "npm run tsc && node app/index.js",
        "tsc": "tsc"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "typescript": "2.1.6"
      }
    }
    

    *回购:https://github.com/pluralsight-courses/typescript-fundamentals/tree/master/001-GettingStarted

    【讨论】:

      【解决方案2】:

      您也可以使用prestart 脚本。默认情况下,它在启动命令之前运行(查看您可以设置的所有默认脚本here)。

        "scripts": {
          "prestart": "npm run tsc",
          "start": "node app/index.js",
          "tsc": "tsc"
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2017-02-13
        • 1970-01-01
        • 2019-07-17
        • 1970-01-01
        相关资源
        最近更新 更多