Node.js Cannot find module ‘mysql’ ‘express’

Not only ‘mysql’, but also ‘express’ Cannot find module.
When you use NodeJS, you inevitably refer to modules written by others, just as you must refer to third-party JARS when writing Java code.
At the time of reference, it can be obtained through NPM. At this time, if the location of execution command is not correct, it will cause the error of Cannot find Module ‘mysql’.
 
Different command locations will make the module installation location different, because there is no -g in the command, will be installed under the current path:
(Both 1 and 2 are in the wrong installation position, and the 3 bits are in the correct installation position. The reason is at the end.)
1. Execute command at any location: NPM install mysql-g. This module will appear in The directory C:\Users\SMouse\AppData\ NPM \ Node-Modules, as shown in the figure below
(Execute command)

(File location)

2. NPM install mysql in nodeJS installation directory. The ‘mysql’ module will be installed in the following node_modules

3. NPM install mysql in the project code, and the ‘mysql’ module will be installed in the following Node_modules (one will be automatically generated without this file).
(The following node_modules is where the modules are in my project. The rest of the files are code for various projects.)


mysql directory has a mysql. Js test code as follows:
var mysql = require(‘mysql’);

:
At first I thought I was going to put all the modules in nodejs in the node_modules directory. If the project code is on disk, it is ok, but if NodeJS is installed on disk D and the project code is on disk E, then the module is not referenced. C:\Users\SMouse\AppData\ NPM \ Node-Modules Path is the same.
The following three steps occur when a custom module (non-core module) is referenced in Node
1. Path analysis
2. File positioning
3. Compile and execute
The path analysis is carried out in the following way:
1) Node_modules in the current directory,
2) Node_modules in the parent directory;
3) Node_modules in the parent directory;
4) Recurse step by step until node_modules in the root directory.
If the module referenced in the code is not found in any of these directories, an error is reported: Cannot find Module ‘mysql’

— — — — — — — — — — — — — — — — — — — — —
the author: SMouse fish bones
source: CSDN
,
https://blog.csdn.net/fouglelove/article/details/52778021 copyright statement: this article original articles for bloggers, reproduced please attach link to blog!

Read More: