简体   繁体   中英

Problem when i run npm run dev in my node backend

I try to build API with node,and i have my frontend separated in folder called client,and this files(package.json,server.js is in my project root folder. When i want to npm run dev,it gives me this error:

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.

Here is my 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 });
    }
  });
});

and my 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"
  }
}

I try to delete my lock file and node_modules and npm cache clean ,but they don't help

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

Your npm run dev script calls npm server , which it tries to call nodemon server.js , but apparently the nodemon is not installed in your project. See your list of dependencies, and install nodemon, or remove it from server script.
Apparently it should work ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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