0

The index.js file has the following code as;

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();

app.use(morgan('tiny'));
app.use(cors());
app.use(bodyParser.json());

app.get('/', (req, res) => {
    res.json({
        message: 'Welcome!'
    });
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
    console.log(`Listening on port ${port}...`);
});

And I edited the package.json

"main": "index.js",
  "scripts": {
    "start": "node index.js"
  },

The dependecies versions are:

"dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "morgan": "^1.10.0"
  }

When I run npm start on terminal, the error shows like this:

npm ERR! code ENOENT npm ERR! syscall open npm ERR! path E:\.....\package.json npm ERR! errno -4058 npm ERR! enoent ENOENT: no such file or directory, open 'E:\......\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Mamun\AppData\Roaming\npm-cache\_logs\2020-04-15T10_03_09_445Z-debug.log

What can I do? Please help. Thanks in advance!

mdmostafa
  • 618
  • 9
  • 19
  • you have to run `npm run start` instead of `npm start` – Dorian B Apr 15 '20 at 10:23
  • 1
    Make sure you are located in your directory with the `package.json` file. It seems you are not in it. – Mickael B. Apr 15 '20 at 10:32
  • 2
    @DorianB for start it's ok to just use `npm start`: https://stackoverflow.com/questions/51358235/difference-between-npm-start-and-npm-run-start#answer-51358329 – Getter Jetter Apr 15 '20 at 10:39
  • @MickaelB. Yes, my directory on terminal was wrong. Correcting this, I run again. Now it shows like this: `events.js:288 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE: address already in use :::3000 ←[90m at Server.setupListenHandle [as _listen2] (net.js:1309:16)←[39m pm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! server@1.0.0 start: `node index.js` npm ERR! Exit status 1 npm ERR! Failed at the server@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above...` – mdmostafa Apr 15 '20 at 10:40
  • 1
    @mostafa.bd this means that port 3000 is already used by another process. Do you have your server already running somewhere else? – Getter Jetter Apr 15 '20 at 10:41
  • 1
    @OlivierKrull Yes, changed the port and now it works well. Thank you! – mdmostafa Apr 15 '20 at 10:45
  • 1
    Thank you very much for all of your support. – mdmostafa Apr 15 '20 at 10:46

1 Answers1

0

The error you are getting here specifies on that line that the package.json file is not found :

npm ERR! enoent ENOENT: no such file or directory, open 'E:......\package.json'

You need to run your npm start command from the folder in which there is your package.json file.

stacks
  • 349
  • 2
  • 5
  • 13