Home > database >  Cannot find module when main file in src folder
Cannot find module when main file in src folder

Time:06-09

I got this error "Cannot find module 'D:\utalk-project\server\server.js'" when I tried to run npm start in my server. But when I move file server.js out of src folder, i can run my server. I want to move server.js into src folder without the error, pls tell me..

my package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "/src/server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon 'server.js'"
  },  
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/core": "7.15.5",
    "@babel/node": "7.15.4",
    "@babel/preset-env": "7.15.6",
    "body-parser": "1.19.0",
    "dotenv": "10.0.0",
    "express": "4.17.1",
    "multer": "^1.4.3",
    "mysql2": "2.3.3",
    "nodemon": "2.0.12",
    "routes": "^2.1.0",
    "sequelize": "^6.20.1",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@types/uuid": "^8.3.4",
    "nyc": "^15.1.0",
    "sequelize-cli": "^6.2.0"
  }
}

my server.js

import express from 'express';
import initrouter from './models/router.js';
// import APIrouter from './models/api.js';
import connectDB from './config/connectDB.js';
import 'dotenv/config';

const app = express();
const port = process.env.PORT || 3001; 

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

connectDB();

app.use('/', initrouter);
// app.use('/api/v1',  APIrouter);

app.listen(port, () => {
    console.log(`Example app listening at http:/localhost:${port}`)
})

my src folder

CodePudding user response:

when going into folders you want to do so like this:

"main": "folder/file.js"

CodePudding user response:

Main should be:

"main": "./src/server.js",

And your npm start the path is wrong it should be:

"start": "nodemon './src/server.js'"

The above assumes in your directory server the package.json exists.

  • Related