我已经在 Ubuntu VPS 上成功部署了我的 Node.js 项目,但是当我尝试运行它时,它显示了一个错误
Error: Cannot find module 'express'
- 如 (1) 所示,即使我已经安装了 Express,它也会给出另一个错误,如下所示如图(2)所示。安装 Express 后,当我尝试运行 node app.js
时,它会出现另一个错误,如 (3) 所示。
当我开始运行“node app.js”时,它给出以下错误:未找到“express”。
root@GreenCloud:/home/test-tnp# node app.js
node:internal/modules/cjs/loader:1093
throw err;
^
Error: Cannot find module 'express'
Require stack:
- /home/test-tnp/app.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1090:15)
at Module._load (node:internal/modules/cjs/loader:934:27)
at Module.require (node:internal/modules/cjs/loader:1157:19)
at require (node:internal/modules/helpers:119:18)
at Object.<anonymous> (/home/test-tnp/app.js:1:17)
at Module._compile (node:internal/modules/cjs/loader:1275:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
at Module.load (node:internal/modules/cjs/loader:1133:32)
at Module._load (node:internal/modules/cjs/loader:972:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/home/test-tnp/app.js' ]
}
之后,我尝试安装“express”库,发现存在与某些依赖项相关的错误,如下所示。
root@GreenCloud:/home/test-tnp# npm install express
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '[email protected]',
npm WARN EBADENGINE required: { node: '>= 0.10.0 < 0.11.0' },
npm WARN EBADENGINE current: { node: 'v19.9.0', npm: '9.6.3' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '[email protected]',
npm WARN EBADENGINE required: { node: '0.8.x' },
npm WARN EBADENGINE current: { node: 'v19.9.0', npm: '9.6.3' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '[email protected]',
npm WARN EBADENGINE required: { node: '0.8.x' },
npm WARN EBADENGINE current: { node: 'v19.9.0', npm: '9.6.3' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '[email protected]',
npm WARN EBADENGINE required: { node: '>= 0.8.0 < 0.11.0' },
npm WARN EBADENGINE current: { node: 'v19.9.0', npm: '9.6.3' }
npm WARN EBADENGINE }
npm WARN deprecated [email protected]: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
npm WARN deprecated [email protected]: Use uuid module instead
npm WARN deprecated [email protected]: use String.prototype.padStart()
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN tarball tarball data for emitter@http://github.com/component/emitter/archive/1.0.1.tar.gz (sha512-k3Da+QreMb9waaGCHNAHox5QqxnZEYlQmvIVYwQibrI6OpIRyIIyFGgDV5dXRLr1AJ32JLqEh0VxQEq20dFskw==) seems to be corrupted. Trying again.
npm WARN deprecated [email protected]: jsSHA versions < 3.0.0 will no longer receive feature updates
npm ERR! code EINTEGRITY
npm ERR! sha512-k3Da+QreMb9waaGCHNAHox5QqxnZEYlQmvIVYwQibrI6OpIRyIIyFGgDV5dXRLr1AJ32JLqEh0VxQEq20dFskw== integrity checksum failed when using sha512: wanted sha512-k3Da+QreMb9waaGCHNAHox5QqxnZEYlQmvIVYwQibrI6OpIRyIIyFGgDV5dXRLr1AJ32JLqEh0VxQEq20dFskw== but got sha512-r/UcFj7JS3lRjv9cgYjgpDNbAsGUdqU64n6ZUOgSF7s1UFBbGu7pUDwKEjHu9NBCy6j2AmmjNW4rijR4De65eA==. (2809 bytes)
当我安装 Express 后尝试再次运行时,应用程序又报告了一个错误,没有找到“index.js”文件。
root@GreenCloud:/home/test-tnp# node app.js
node:internal/modules/cjs/loader:456
throw err;
^
Error: Cannot find module '/home/test-tnp/node_modules/debug/src/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:448:19)
at Module._findPath (node:internal/modules/cjs/loader:692:18)
at Module._resolveFilename (node:internal/modules/cjs/loader:1076:27)
at Module._load (node:internal/modules/cjs/loader:934:27)
at Module.require (node:internal/modules/cjs/loader:1157:19)
at require (node:internal/modules/helpers:119:18)
at Object.<anonymous> (/home/test-tnp/node_modules/finalhandler/index.js:14:13)
at Module._compile (node:internal/modules/cjs/loader:1275:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
at Module.load (node:internal/modules/cjs/loader:1133:32) {
code: 'MODULE_NOT_FOUND',
path: '/home/test-tnp/node_modules/debug/package.json',
requestPath: 'debug'
}
我的
package.json
文件:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"dev": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"bson": "^5.3.0",
"company-email-validator": "^1.0.9",
"country-code-lookup": "^0.0.22",
"cryptocurrency-address-detector": "^1.1.2",
"dnsbl": "^3.2.0",
"dotenv": "^16.0.3",
"email-provider": "^1.0.1",
"email-syntax-check": "^1.1.3",
"express": "^4.18.2",
"express-validator": "^6.15.0",
"ip-to-hosting": "^1.0.6",
"jsonwebtoken": "^9.0.0",
"legit": "^1.0.9",
"libphonenumber-geo-carrier": "^1.2.6",
"libphonenumber-js": "^1.10.24",
"mail-listener5": "^2.1.2",
"mongoose": "^6.8.3",
"mongoose-unique-validator": "^3.1.0",
"nodemon": "^2.0.20",
"trezor-address-validator": "^0.4.3",
"uuid": "^9.0.0"
}
}
当我运行“npm ls express”时,它在下面显示新错误
root@GreenCloud:/home/test-tnp# npm ls express
npm ERR! code ELSPROBLEMS
npm ERR! invalid: express@ /home/test-tnp/node_modules/express
npm ERR! invalid: express@ /home/test-tnp/node_modules/tomahawk/node_modules/express
npm ERR! extraneous: express@ /home/test-tnp/node_modules/tomahawk/node_modules/express
[email protected] /home/test-tnp
├── express@ invalid: "^4.19.2" from the root project
└─┬ [email protected] extraneous
└── express@ invalid: "4.6.1" from node_modules/tomahawk extraneous
当我将 Node.js 更新到版本 20.x(之前的版本是 16.x)并将 npm 更新到 10.x 时,它解决了该问题。运行它不再遇到错误。谢谢大家对这个话题的关注!