【问题标题】:Problem when i run npm run dev in my node backend当我在我的节点后端运行 npm run dev 时出现问题
【发布时间】:2020-05-11 03:10:02
【问题描述】:

我尝试使用节点构建 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 ,但它们没有帮助

【问题讨论】:

    标签: node.js reactjs stripe-payments backend


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我已经全局安装了,仍然报同样的错误:(
      猜你喜欢
      • 2019-02-22
      • 2017-06-15
      • 2017-11-11
      • 1970-01-01
      • 2018-12-12
      • 2021-11-14
      • 1970-01-01
      • 2020-05-12
      • 2023-02-02
      相关资源
      最近更新 更多