This article is showing how to solve a certain error message. The error message appear upon executing a NodeJS application. The execution of the NodeJS application is actually just a simple one. The error message exist as follows :
[admin@10 nodejs]$ node app.js internal/modules/cjs/loader.js:883 throw err; ^ Error: Cannot find module 'express' Require stack: - /home/admin/nodejs/app.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15) at Function.Module._load (internal/modules/cjs/loader.js:725:27) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object. (/home/admin/nodejs/app.js:1:17) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/admin/nodejs/app.js' ] } [admin@10 nodejs]$
So, there is a file with the name of ‘app.js’ in the folder of ‘/home/admin’. The content of the file is in the following line :
const express = require("express"); console.debug("Test NodeJS !");
The above line is just a syntax for using a module. That module exist as the parameter of the function. In the above line, the module name is ‘express’. Just read the article in this link with the title of ‘How NodeJS Require works!’ and this link for further information about the require function.
But apparently, although the function is correct for using a specific module with the name of ‘express’, it does not exist. The error message exist as the above node application execution :
code: 'MODULE_NOT_FOUND',
So, the solution is just to install the module. The following is the execution of installing the module :
[admin@10 nodejs]$ npm install express --save added 50 packages, and audited 51 packages in 6s found 0 vulnerabilities [admin@10 nodejs]$
After executing the above command, there are several files and folders exist as in the following output :
[admin@10 nodejs]$ ls -al total 44 drwxrwxr-x 3 admin admin 85 Mar 4 12:34 . drwx------. 5 admin admin 184 Mar 4 12:33 .. -rw-rw-r-- 1 admin admin 68 Mar 4 12:33 app.js drwxrwxr-x 52 admin admin 4096 Mar 4 12:34 node_modules -rw-rw-r-- 1 admin admin 53 Mar 4 12:34 package.json -rw-rw-r-- 1 admin admin 31168 Mar 4 12:34 package-lock.json [admin@10 nodejs]$
Apparently, after installing the ‘express’ module and trying to execute the application again, the following output will appear :
[admin@10 nodejs]$ node app.js Test NodeJS ! [admin@10 nodejs]$