编辑 #1:似乎我有一个有效的配置,但是欢迎所有改进它的建议。查看答案:https://stackoverflow.com/a/42269408/1155847
原始问题:
我目前正在尝试设置我的环境,以便使用我的 package.json
的 devDependencies
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/