richardthefourth
richardthefourth

Reputation: 123

Cannot find module app.js?

I am starting node app.js in the server directory, and I get the following:

module.js:544
    throw err;
    ^

Error: Cannot find module 'server/server/app.js'
    at Function.Module._resolveFilename (module.js:542:15)
    at Function.Module._load (module.js:472:25)
    at Function.Module.runMain (module.js:682:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:613:3
[nodemon] app crashed - waiting for file changes before starting...

How to fix this? It was working before, and now it's looking for a weird path /server/server instead of just /server.

I just reinstalled node to a more up-to-date version, from 5. to latest stable.

Upvotes: 4

Views: 20848

Answers (3)

Eka putra
Eka putra

Reputation: 759

maybe you want to access file app.js,

require('./server/server/app.js')
// to be like this
require('server/server/app.js')

use can using this package

npm i app-module-path

Upvotes: 0

WasiF
WasiF

Reputation: 28847

I encountered with this issue because I deleted build folder and then try to build the project.

Solution - in my case

I run the tsc --watch that compiled all .ts files to .js

Upvotes: 0

JSchirrmacher
JSchirrmacher

Reputation: 3354

Most likely you try to load your module like so:

require('server/server/app.js')

Node.js then tries to find it somewhere below node_modules. But I suppose, the file is not a dependency but part of your own code. Normally, you would then require it like so (consider the point at the beginning):

require('./server/server/app.js')

This makes Node.js look for the file beginning at the current directory ('.').

This would work in an environment with the following folder structure:

/myfiles/main.js   <-- this file contains the `require` statement
/myfiles/server/
/myfiles/server/server/
/myfiles/server/server/app.js   <-- this file is being `require`d

Upvotes: 5

Related Questions