NPM模块在/node_modules文件夹中,但无法找到?

7

我在设置好的Ubuntu开发服务器上安装md5节点模块时遇到了一些问题。

(在我的本地Windows机器上正常工作,并且我可以在开发服务器上使用npm安装其他模块)

当我尝试启动正在开发的NodeJS应用程序时,它会失败并指出md5模块未安装。

    // trying to start my application that depends on md5.
    drichardson@devwebserver:/var/www/node_app/meanapp$ node server.js
    module.js:340
        throw err;
        ^
    Error: Cannot find module 'md5'
        at Function.Module._resolveFilename (module.js:338:15)
        at Function.Module._load (module.js:280:25)
        at Module.require (module.js:364:17)
        at require (module.js:380:17)
        at Object.<anonymous> (/var/www/node_app/node_modules/hashfile/index.js:7:11)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Module.require (module.js:364:17)
        at require (module.js:380:17)
        at Object.<anonymous> (/var/www/node_app/meanapp/app/controllers/exchanges.server.controller.js:15:14)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)

我尝试将其安装在我的工作目录中:

    // Installing in working directory
    drichardson@devwebserver:/var/www/node_app/meanapp$ sudo npm install --save md5
    npm http GET https://registry.npmjs.org/md5
    npm http GET https://registry.npmjs.org/md5
    npm http 304 https://registry.npmjs.org/md5
    npm http GET https://registry.npmjs.org/is-buffer
    npm http GET https://registry.npmjs.org/crypt
    npm http GET https://registry.npmjs.org/charenc
    npm http 304 https://registry.npmjs.org/crypt
    npm http GET https://registry.npmjs.org/is-buffer
    npm http GET https://registry.npmjs.org/charenc
    npm http 200 https://registry.npmjs.org/is-buffer
    npm http GET https://registry.npmjs.org/is-buffer/-/is-buffer-1.0.2.tgz
    npm http GET https://registry.npmjs.org/is-buffer/-/is-buffer-1.0.2.tgz
    npm http GET https://registry.npmjs.org/charenc
    npm http GET https://registry.npmjs.org/charenc/-/charenc-0.0.1.tgz
    npm http 200 https://registry.npmjs.org/charenc/-/charenc-0.0.1.tgz
    npm http GET https://registry.npmjs.org/is-buffer/-/is-buffer-1.0.2.tgz
    npm http 200 https://registry.npmjs.org/is-buffer/-/is-buffer-1.0.2.tgz
    md5@2.0.0 node_modules/md5
    ├── charenc@0.0.1
    ├── crypt@0.0.1
    └── is-buffer@1.0.2

尽管节点模块显示在我的/node_modules文件夹中,它仍会抛出“找不到模块md5”的错误消息。
    // MD5 In my node_modules folder:
    drichardson@devwebserver:/var/www/node_app/meanapp/node_modules$ ls -la | grep 'md5'
    drwxr-xr-x  2 drichardson drichardson 4096 Oct 30 13:53 md5

我还尝试过以下方法:

- 使用“npm install -g md5”全局安装模块
- 清除缓存并重新安装“npm cache clean”
- 使用“npm install”重新安装应用程序

还有什么其他办法可以让npm识别已安装的md5模块吗?

谢谢。


2
请检查安装位置,即使您尝试在工作目录中安装,如果其中一个根目录具有node_modules目录,则会在那里安装而不是在工作目录中。我认为使用“-g”全局安装应该可以正常工作。 - vinayakj
4个回答

6
我曾经遇到过类似的问题,但问题并不在于模块安装的位置,而是因为 package.json 文件中的 main 属性设置不正确。在我的情况下,它引用了一个不存在的文件。请确保模块的 "main":"..." 设置正确。
如果这是一个你无法控制的模块,你可以在最近的 node_modules 目录中进行快速修复。在我们的项目中,它位于根目录下。

1
你可以尝试清除整个node_modules并重新安装所有内容吗?
   rm -rf node_modules
   npm install

我尝试了这个,但它没有起作用。我仍然遇到了“md5”模块未安装的相同问题。不过还是谢谢你。 - Dave Rich

0
在文件夹/var/www/node_app/node_modules/hashfile/中运行npm install md5。你试过了吗?

0

我的问题最终是由于我本地运行的Node和NPM版本与Docker容器中的版本不同造成的。

# Local versions:
$ node --version
v12.18.2
$ npm --version
6.14.10

我在我的Dockerfile中进行了修改

FROM node:15-alpine3.12
#         ^^
# ...

FROM node:12-alpine3.12
#         ^^
# ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接