如何使用 package.json 中的命令启动我的 Express 服务器并使用 nodemon?

huangapple go评论173阅读模式
英文:

how can I start my express server with nodemon with commands in package.json?

问题

I can start my server with nodemon src/server.ts but I want it to start via package.json with a command like npm start.

How can I do it?

I have seen a tutorial but it doesn't work; it shows me an error, the command is wrong:

  1. "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",

from this site
enter link description here

Here is my package.json:

  1. {
  2. "name": "express_inngest",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "start": "ts-node ./src/server.ts",
  8. "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",
  9. "start:prod": "npm run build && node ./dist/src/server.js",
  10. "build": "npx tsc"
  11. },
  12. "keywords": [],
  13. "author": "",
  14. "license": "ISC",
  15. "dependencies": {
  16. "express": "^4.18.2"
  17. },
  18. "devDependencies": {
  19. "@types/cookie-parser": "^1.4.3",
  20. "@types/express": "^4.17.17",
  21. "@types/node": "^20.4.9",
  22. "nodemon": "^3.0.1",
  23. "ts-node": "^10.9.1",
  24. "typescript": "^5.1.6"
  25. }
  26. }

nodemon json:

  1. {
  2. "ignore": [".git", "node_modules", "dist"],
  3. "watch": ["./src"],
  4. "exec": "npm start",
  5. "ext": "ts"
  6. }
英文:

I can start my server with nodemon src/server.ts but I want it start via package.json with command like npm start.

How can I do it ?

I have seen a tutorial but it not works it shows me a error the command is wrong:

  1. "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",

from this site
enter link description here

Here is my package.json

  1. {
  2. "name": "express_inngest",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "start": "ts-node ./src/server.ts",
  8. "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",
  9. "start:prod": "npm run build && node ./dist/src/server.js",
  10. "build": "npx tsc"
  11. },
  12. "keywords": [],
  13. "author": "",
  14. "license": "ISC",
  15. "dependencies": {
  16. "express": "^4.18.2"
  17. },
  18. "devDependencies": {
  19. "@types/cookie-parser": "^1.4.3",
  20. "@types/express": "^4.17.17",
  21. "@types/node": "^20.4.9",
  22. "nodemon": "^3.0.1",
  23. "ts-node": "^10.9.1",
  24. "typescript": "^5.1.6"
  25. }
  26. }

nodemon json

  1. {
  2. "ignore": [".git", "node_modules", "dist"],
  3. "watch": ["./src"],
  4. "exec": "npm start",
  5. "ext": "ts"
  6. }

答案1

得分: 1

这里是一个我使用你的目录结构的配置,没有太多需要解释的。

  1. // package.json
  2. "scripts": {
  3. "start": "nodemon",
  4. "start:raw": "ts-node ./src/server.ts",
  5. "start:prod": "npm run build && node ./dist/src/server.js",
  6. "build": "npx tsc"
  7. },
  1. // nodemon.json
  2. {
  3. "ignore": [".git", "node_modules", "dist"],
  4. "watch": ["./src"],
  5. "exec": "ts-node ./src/server.ts",
  6. "ext": "ts"
  7. }

因此,你可以像这样更新你的配置。

英文:

There is not much to explain, here is a config for I use your directory structure.

  1. //package.json
  2. "scripts": {
  3. "start": "nodemon",
  4. "start:raw": "ts-node ./src/server.ts",
  5. "start:prod": "npm run build && node ./dist/src/server.js",
  6. "build": "npx tsc"
  7. },
  1. // nodemon.json
  2. {
  3. "ignore": [".git", "node_modules", "dist"],
  4. "watch": ["./src"],
  5. "exec": "ts-node ./src/server.ts",
  6. "ext": "ts"
  7. }

so, you can update your config like this

huangapple
  • 本文由 发表于 2023年8月11日 04:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定