node.js - 在 package.json 中配置本地 typescript 编译器

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

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


原始问题:

我目前正在尝试设置我的环境,以便使用我的 package.jsondevDependencies typescript 版本。对此有哪些最佳实践,以便它“编辑器不知道”并且最好可以用作 npm 脚本,例如:npm run tscompile?

要清楚 - 使用 npm install typescript -g 时我可以让一切正常工作 - 但我依赖于全局安装版本,这不是我想要的 - 因为我们会想在团队中工作并在升级前为每个成员设置特定的 typescript 版本,所以我们都在同一页面上。

我目前正在尝试将其设置为这样 - 然而 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"
    ]
}

最佳答案

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

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"
  }
}

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

关于node.js - 在 package.json 中配置本地 typescript 编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42269301/

相关文章:

angular - 在 Angular 2 应用程序中使用接口(interface)和 OpaqueToken 时出现 Typescript 警告

node.js - Mocha 使用异步初始化代码进行测试

node.js - 在 Mac OSX 10.5 上运行 Node.js 或 NPM 时为 "Process out of memory"

javascript - iron-ajax 绑定(bind)并将 JSON 写入文件

node.js - Socket.io 发出的信号并不总是在 Node.js + Redis + Heroku(多测功机)环境中收到

reactjs - 没有找到匹配的文件...。 (ReactJS)

node.js - 如何在 TypeScript 中使用 gulp-load-plugins?

angular - 在可视化代码中调试 Angular/Typescript 将 '_1' 附加到变量

reactjs - 错误 : "createRequire is not a function Referenced from: BaseConfig"

node.js - 在 Ubuntu 20.04 上安装 Node js 时出现 python2 错误