Home > front end >  Error: Cannot find module '/src' when running 'src/'
Error: Cannot find module '/src' when running 'src/'

Time:07-25

I try to start the src/ folder with node (node src/) or nodemon (npm start) to start the three .js files in that path and strangely it does not find the src/ folder

I clarify that the files start perfectly when started separately

"scripts": {
    "start": "nodemon src/"
}

I find it very strange since I start all my projects in the same way and I have never had this problem before.

teyrox@teyrox:~/project$ npm start
...
[nodemon] starting `node src/`
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/project/src'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting..
teyrox@teyrox:~/project$ node src/
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/project/src'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

And yes, I run these commands from the / path of the project

File tree

EDIT: My main property in package.json is src/, I tried changing it to src/server.js but it did not work.

CodePudding user response:

Can you replace scripts inside package.json

"scripts": {
    "start": "nodemon src/server.js"
  }

CodePudding user response:

I changed the name of the server.js file to index.js and it works normally.

Apparently, when starting a directory node looks for index.js by default.

  • Related