[英]Node.js unable to find module(s) outside of an interactive session
我正在運行一個 node:latest 容器,本地磁盤映射到其中,我的項目特定文件所在的位置。 我希望服務器在我從容器外部編輯它們時自動檢測這些文件中的更改 - 這由nodemon
模塊提供支持。
大約 2 年前,我上次這樣做時,這曾經工作得很好。 我采用了那個舊項目,對其進行了更新(除了解決嚴重的安全漏洞之外,讓它現在運行顯然是必要的),但它聲稱從命令行運行 node 時找不到nodemon
模塊(由容器啟動或手動啟動)。 如果我啟動一個交互式節點會話,在進入容器后,它可以找到模塊。
當容器啟動時,模塊是全新安裝的。 至少就目前而言,一旦問題得到解決,這種情況就會改變。
簡短版本:
# node nodemon version
internal/modules/cjs/loader.js:1032
throw err;
^
Error: Cannot find module '/app/nodemon'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
# node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> nm = require("nodemon")
[Function: nodemon] {
restart: [Function (anonymous)],
on: [Function (anonymous)],
addListener: [Function (anonymous)],
once: [Function (anonymous)],
emit: [Function (anonymous)],
removeAllListeners: [Function (anonymous)],
reset: [Function (anonymous)],
config: {
run: false,
system: { cwd: '/app' },
required: true,
dirs: [],
timeout: 1000,
options: {},
load: [Function (anonymous)],
reset: [Function: reset]
}
}
> .exit
長版本,帶有完整性檢查...所有命令都從容器內部運行,在通過 docker-compose.yml 掛載的本地目錄中:
# pwd
/app
# ls
bin data node_modules package-lock.json package.json public routes server.js startScript.sh views
# cat package.json
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"cookie-parser": "^1.4.6",
"debug": "^4.3.4",
"express": "^4.18.1",
"http-errors": "^2.0.0",
"jQuery": "^1.7.4",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^2.0.16",
"pug": "^3.0.2"
}
}
# export NODE_PATH=/app/node_modules
# echo $NODE_PATH
/app/node_modules
# ls $NODE_PATH
@babel binary-extensions color-convert depd function-bind imurmurhash is-regex mime-db on-headers pug-filters registry-url strip-json-comments url-parse-lax
@sindresorhus body-parser color-name destroy get-intrinsic inherits is-typedarray mime-types once pug-lexer resolve supports-color util-deprecate
@szmarczak boxen concat-map doctypes get-stream ini is-yarn-global mimic-response p-cancelable pug-linker responselike supports-preserve-symlinks-flag utils-merge
abbrev brace-expansion concat-stream dot-prop glob-parent ipaddr.js isarray minimatch package-json pug-load safe-buffer to-fast-properties vary
accepts braces configstore duplexer3 global-dirs is-binary-path jQuery minimist parseurl pug-parser safer-buffer to-readable-stream void-elements
acorn buffer-from constantinople ee-first got is-ci js-stringify mkdirp path-parse pug-runtime semver to-regex-range widest-line
ansi-align busboy content-disposition emoji-regex graceful-fs is-core-module json-buffer morgan path-to-regexp pug-strip-comments semver-diff toidentifier with
ansi-regex bytes content-type encodeurl has is-expression jstransformer ms picomatch pug-walk send token-stream wrap-ansi
ansi-styles cacheable-request cookie end-of-stream has-flag is-extglob keyv multer prepend-http pump serve-static touch wrappy
anymatch call-bind cookie-parser escape-goat has-symbols is-fullwidth-code-point latest-version negotiator process-nextick-args pupa setprototypeof type-fest write-file-atomic
append-field camelcase cookie-signature escape-html has-tostringtag is-glob lowercase-keys nodemon promise qs side-channel type-is xdg-basedir
array-flatten chalk core-util-is etag has-yarn is-installed-globally lru-cache nopt proxy-addr range-parser signal-exit typedarray xtend
asap character-parser crypto-random-string express http-cache-semantics is-npm make-dir normalize-path pstree.remy raw-body statuses typedarray-to-buffer yallist
assert-never chokidar debug fill-range http-errors is-number media-typer normalize-url pug rc streamsearch undefsafe
babel-walk ci-info decompress-response finalhandler iconv-lite is-obj merge-descriptors object-assign pug-attrs readable-stream string-width unique-string
balanced-match cli-boxes deep-extend forwarded ignore-by-default is-path-inside methods object-inspect pug-code-gen readdirp string_decoder unpipe
basic-auth clone-response defer-to-connect fresh import-lazy is-promise mime on-finished pug-error registry-auth-token strip-ansi update-notifier
# npm view nodemon
nodemon@2.0.16 | MIT | deps: 10 | versions: 237
Simple monitor script for use during development of a Node.js app.
https://nodemon.io
keywords: cli, monitor, monitor, development, restart, autoload, reload, terminal
bin: nodemon
dist
.tarball: https://registry.npmjs.org/nodemon/-/nodemon-2.0.16.tgz
.shasum: d71b31bfdb226c25de34afea53486c8ef225fdef
.integrity: sha512-zsrcaOfTWRuUzBn3P44RDliLlp263Z/76FPoHFr3cFFkOz0lTPAcIw8dCzfdVIx/t3AtDYCZRCDkoCojJqaG3w==
.unpackedSize: 196.5 kB
dependencies:
chokidar: ^3.5.2 debug: ^3.2.7 ignore-by-default: ^1.0.1 minimatch: ^3.0.4 pstree.remy: ^1.1.8 semver: ^5.7.1 supports-color: ^5.5.0 touch: ^3.1.0 undefsafe: ^2.0.5 update-notifier: ^5.1.0
maintainers:
- remy <remy@leftlogic.com>
dist-tags:
debug-1268: 1.15.2-alpha.2 debug: 2.0.14-alpha.1 latest: 2.0.16
published a month ago by remy <remy@leftlogic.com>
# node nodemon version
internal/modules/cjs/loader.js:1032
throw err;
^
Error: Cannot find module '/app/nodemon'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
# node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> nm = require("nodemon")
[Function: nodemon] {
restart: [Function (anonymous)],
on: [Function (anonymous)],
addListener: [Function (anonymous)],
once: [Function (anonymous)],
emit: [Function (anonymous)],
removeAllListeners: [Function (anonymous)],
reset: [Function (anonymous)],
config: {
run: false,
system: { cwd: '/app' },
required: true,
dirs: [],
timeout: 1000,
options: {},
load: [Function (anonymous)],
reset: [Function: reset]
}
}
> .exit
# export NODE_PATH=/app
# echo $NODE_PATH
/app
# node nodemon
internal/modules/cjs/loader.js:1032
throw err;
^
Error: Cannot find module '/app/nodemon'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
#
因為我希望有人會問,盡管看起來無關緊要,這里是安裝模塊的腳本:
#!/bin/sh
rm /app/package.json
rm /app/package-lock.json
rm -rf /app/node_modules
npm init -y
npm install express
npm install http-errors
npm install pug
npm install morgan
npm install cookie-parser
npm install debug
npm install jQuery
npm install body-parser
npm install multer
npm install nodemon
npm audit
npm audit fix
該腳本在一個新的 node:latest 容器上運行,除了上面顯示的目錄中的映射之外沒有其他怪癖,所以這應該是可重現的。 理想情況下,它不會刪除 node_modules 和諸如此類的東西,以加快啟動速度,但在這種情況下我已經這樣做了,只是為了確保一切都是干凈的。
而且,如果有人問,這里是 docker-compose.yml:
#redis:
# image: redis
# volumes:
# - ./webapp/data/redis:/data
#mysql:
# image: mysql:latest
# environment:
# MYSQL_ROOT_PASSWORD: StackOverflowsdfj87ey2,238n
# MYSQL_DATABASE: StackOverflowDB
# MYSQL_USER: StackOverflow
# MYSQL_PASSWORD: StackOverflowmcxhdie73.6xm
# volumes:
# - ./webapp/data/mysql:/var/lib/mysql
application:
image: node:latest
working_dir: /app
command: /app/startScript.sh
volumes:
- ./webapp:/app
ports:
- 80:3000
# links:
# - redis
# - mysql
這就是我能想到的可能與這里相關的所有內容......就像我說的那樣,這曾經可靠地工作 - 使用完全相同的 docker-compose.yml 和啟動腳本,並具有完整的數據庫支持。
Nodemon 不是一個模塊。 通常它是全局安裝的,並用作命令行中的命令。
由於它不是全局安裝的,因此運行它的一種方法是像這樣啟動它
./node_modules/nodemon/bin/nodemon.js -h
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.