如何运行nodemon + ts-node + typescript而不必全局安装ts-node或npx?

uwopmtnx  于 2023-03-19  发布在  TypeScript
关注(0)|答案(3)|浏览(162)

我的package.json中包含以下内容:

"scripts": {
    "serve-fake-api": "nodemon fake-api/server.ts --watch 'fake-api/*.*'",
    "serve-vue": "vue-cli-service serve",
    "serve": "concurrently -k \"npm run serve-fake-api\" \"npm run serve-vue\"",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

我想重写"serve-fake-api": "nodemon --exec 'ts-node' fake-api/server.ts --watch fake-api/*.*",,但不必全局安装ts-nodenpx
我怎样才能做到呢?

ttvkxqim

ttvkxqim1#

您可以只在本地安装nodemonts-node作为开发依赖项:

npm install -D ts-node nodemon

然后跑

nodemon -x 'node -r ts-node/register' fake-api/server.ts --watch 'fake-api/*.*'

现在,当您运行npm脚本时,它们将默认使用您的本地版本:
除了shell预先存在的PATH之外,npm run还会将node_modules/.bin添加到为脚本提供的PATH中。本地安装的依赖项提供的任何二进制文件都可以在没有node_modules/.bin前缀的情况下使用

6yt4nkrj

6yt4nkrj2#

我最近遇到了ts-node-dev,它将ts-nodenode-dev融合在一起。
CLI参数是ts-nodenode-dev接受的参数的组合,在此基础上还有一些自己的参数。
像这样跑:

"start": "tsnd fake-api/server.ts"
6pp0gazn

6pp0gazn3#

我设法用下面的packages.json运行了所有程序:

{
  "name": "rm-combo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve-fake-api": "nodemon fake-api/index.ts --watch fake-api/*.*",
    "serve-vue": "vue-cli-service serve",
    "serve": "concurrently -k \"npm run serve-fake-api\" \"npm run serve-vue\"",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@types/node": "^12.12.7",
    "axios": "~0.19.0",
    "devextreme": "19.2.3",
    "devextreme-vue": "19.2.3",
    "element-ui": "~2.8.2",
    "oidc-client": "~1.9.1",
    "vue": "^2.6.10",
    "vue-class-component": "^7.1.0",
    "vue-property-decorator": "^8.3.0",
    "vue-router": "^3.1.3",
    "vuetify": "^2.1.10",
    "vuex": "^3.1.2",
    "vuex-class": "^0.3.2"
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "@types/json-server": "^0.14.2",
    "@vue/cli-plugin-typescript": "^4.0.5",
    "@vue/cli-service": "^4.0.5",
    "concurrently": "^5.0.0",
    "devextreme-cldr-data": "^1.0.2",
    "globalize": "^1.4.2",
    "json-server": "^0.15.1",
    "node-sass": "^4.13.0",
    "nodemon": "^1.19.4",
    "sass-loader": "^8.0.0",
    "ts-node": "^8.5.0",
    "typescript": "^3.7.2",
    "vue-template-compiler": "^2.6.10"
  }

和TS支持的功能来实现这个技巧:https://stackoverflow.com/a/59126595/4636721

相关问题