当我在我的节点后端运行 npm 运行开发时出现问题



我尝试使用node构建API,并将我的前端分开在名为client的文件夹中,并且此文件(package.json,server.js在我的项目根文件夹中。 当我想npm运行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.

这是我的服务器.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 });
}
});
});

和我的包.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 缓存,但它们没有帮助

在你的 package.json 文件中,它应该是 "dev": "con并发 --kill-others-on-fail

\"npm run server\" \"npm run client\"

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

最新更新