0

I know that this has been asked before but none of the solutions worked for me. Whenever I try to install express, it seems to work just fine, displaying this message:

npm WARN website@1.0.0 No description

+ express@4.17.1 updated 1 package and audited 50 packages in 6.581s found 0 vulnerabilities

However, when I try to run my program that uses express:

 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (C:\Users\darcy\OneDrive\Sutton\Website\server.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
  code: 'MODULE_NOT_FOUND',`

Here is my code:

let express = require("express");
let account = require("accountBack");
let shop = require("shopBack")

let serverConnection = express();
serverConnection.listen(3000, () => {});
serverConnection.use(express.static("public"));
serverConnection.use(express.json());
serverConnection.post("/account", account.accountServer);
serverConnection.get("/shop", shop.sendShop);

I am using code from other files (hence the extra 2 require statements) but what they do I do not believe to be significant.

Here are the commands that I am typing in:

npm init
npm install express
node server.js

It does not appear from the error that express is gettinginstalled correctly, but I'm not sure. Is it a problem with the commands that I a typing in or something else? Any help would be appreciated.

Andy
  • 61,948
  • 13
  • 68
  • 95
Darcy Sutton
  • 111
  • 6
  • Oops, line 4 was meant to be displayed as code. – Darcy Sutton Sep 28 '21 at 23:41
  • https://stackoverflow.com/questions/47083351/node-module-not-found – Andy Sep 28 '21 at 23:48
  • Does this help? https://stackoverflow.com/questions/5919629/express-module-not-found-when-installed-with-npm – pxDav Sep 28 '21 at 23:52
  • According to the backtrace this is arising from line 2 of your`server.js` file, are you sure it isn't one of the other requires? – msbit Sep 29 '21 at 00:18
  • @msbit No, I double-checked it still doesn't work. – Darcy Sutton Sep 29 '21 at 00:39
  • Sorry, neither of those links help. Also, @Andy are you the guy who made the self-retweeting tweet? – Darcy Sutton Sep 29 '21 at 00:43
  • @DarcySutton That would be a different Andy - we're everywhere :) – Andy Sep 29 '21 at 00:47
  • Fairly confident, given what you have described, that it is failing on the inclusion of `accountBack`. Copying your code, running the commands you've described, even with `accountBack` and `shopBack` files in place gives me literally the following error: `Error: Cannot find module 'accountBack'` followed by the approximately same backtrace. – msbit Sep 29 '21 at 04:48
  • @Andy ok haha :) – Darcy Sutton Sep 29 '21 at 11:42

1 Answers1

1

For the internal module, you should import like this

const account = require("./accountBack");
const shop = require("./shopBack")

Unless you publish those modules to npm registry (private/public), then you can import like what you did.

xwlee
  • 1,083
  • 1
  • 11
  • 29
  • 1
    This. As per https://nodejs.org/en/knowledge/getting-started/what-is-require/: `The rules of where require finds the files can be a little complex, but a simple rule of thumb is that if the file doesn't start with "./" or "/", then it is either considered a core module (and the local Node.js path is checked), or a dependency in the local node_modules folder. If the file starts with "./" it is considered a relative file to the file that called require. If the file starts with "/", it is considered an absolute path. NOTE: you can omit ".js" and require will automatically append it if needed.` – msbit Sep 29 '21 at 04:42
  • Works but it returns a different error now. It is a simple one so I should be able to figure it out. Thx for the help! – Darcy Sutton Sep 29 '21 at 11:48