0

这是 package.json 中的依赖项

{
  "name": "whatsapp",
  "version": "1.0.0",
  "description": "whatsapp-clone-in-mern-stack",
  "main": "server.js",
  "scripts": {
    "start": "./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "someone",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-preset-node8": "^1.2.0",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "moment": "^2.29.1",
    "mongoose": "^6.0.14"
  }
}

server.js 文件中的代码

    import App from "express";
    import connectDB from "./dbConnection";
     const app = new App();
    
     const PORT = 3001;
    
     const startServer = () => {
         Promise.all([connectDB()]).then(() => {
             app.listen(PORT);
             console.log(`Server started on port ${PORT}`);
         });
     };
    
     startServer();

index.js 文件中的代码

import mongoose from "mongoose";

const DB_CONNECTION_URL = "mongodb://localhost:27017/test";

const connectDB = () => {
    console.log("DB trying to connect on " + new Date());
    const options = {
        keepAlive: 1,
        autoReconnect: true,
        poolSize: 10,
        useNewUrlParser: true,
        useUnifiedTopology: true,
    };
    return mongoose.connect(DB_CONNECTION_URL, options);
};

export default connectDB;

控制台中的错误

    whatsapp@1.0.0 start C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server
    > ./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js
    
    '.' is not recognized as an internal or external command,
    operable program or batch file.
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! whatsapp@1.0.0 start: `./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the whatsapp@1.0.0 start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\krish\AppData\Roaming\npm-cache\_logs\2021-11-30T

09_26_47_364Z-debug.log

从 YouTube 教程中学习,我尝试制作一个 whatsapp 克隆。我想使用 babel cli 和 babel node 来启动服务器以便连接数据库,所以我写了这段代码 { "./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server. js" } 中,但它显示上述错误。请帮我。

4

2 回答 2

1

您遇到的错误是由于在 Windows 中使用 *nix 路径语法造成的。尝试使用 Windows 反斜杠编写路径或尝试 babel-node --presets node8 .\server.js

于 2021-11-30T15:05:24.707 回答
0

写这个

"scripts": {
    "start": "node ./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }, 

而不是这个

"scripts": {
        "start": "./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
于 2021-11-30T15:36:45.663 回答