繁体   English   中英

我在节点后端运行 npm run dev 时出现问题

[英]Problem when i run npm run dev in my node backend

我尝试使用 node 构建 API,并且我将前端分隔在名为 client 的文件夹中,并且这个文件(package.json,server.js 在我的项目根文件夹中。当我想 npm run dev 时,它给了我这个错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! clothing-server@1.0.0 dev: `concurrently --kill-others-on-fail "npm server" "npm client"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the clothing-server@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

这是我的 server.js:

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');

if (process.env.NODE_ENV !== 'production') require('dotenv').config();

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

const app = express();

const port = process.env.PORT || 5000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

if (process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, 'client/build')));

  app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
  });
}
app.listen(port, error => {
  if (error) throw error;
  console.log(`Server running on ${port}`);
});

app.post('/payment', (req, res) => {
  const body = {
    source: req.body.token.id,
    amount: req.body.amount,
    currency: 'usd'
  };
  stripe.charges.create(body, (stripeErr, stripeRes) => {
    if (stripeErr) {
      res.status(500).send({ error: stripeErr });
    } else {
      res.status(200).send({ error: stripeRes });
    }
  });
});

和我的 package.json:

 {
  "name": "clothing-server",
  "version": "1.0.0",
  "engines": {
    "node": "10.16.0",
    "npm": "6.9.0"
  },
  "scripts": {
    "client": "cd client && npm start",
    "server": "nodemon server.js",
    "build": "cd client && npm run build",
    "dev": "concurrently --kill-others-on-fail \"npm server\" \"npm client\"",
    "start": "node server.js",
    "heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "compression": "1.7.4",
    "cors": "2.8.5",
    "dotenv": "8.2.0",
    "express": "^4.17.1",
    "stripe": "8.6.0"
  },
  "devDependencies": {
    "concurrently": "^5.0.2"
  }
}

我尝试删除我的锁定文件和 node_modules 和 npm cache clean ,但它们没有帮助

在你的 package.json 文件中,它应该是 "dev": "concurrently --kill-others-on-fail \\"npm run server\\" \\"npm run client\\""

您的npm run dev脚本调用npm server ,它尝试调用nodemon server.js ,但显然您的项目中未安装 nodemon 。 查看您的依赖项列表,并安装 nodemon,或从服务器脚本中删除它。
显然它应该工作;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM