简体   繁体   English

无法访问全局安装的节点模块

[英]Not able to access globally installed node module

I tried to install @hapi/joi using command我尝试使用命令安装@hapi/joi

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ sudo npm install @hapi/joi -g
+ @hapi/joi@16.1.7
updated 1 package in 2.833s

I am able to see installed @hapi/joi in /usr/local/lib/node_modules我可以在/usr/local/lib/node_modules中看到已安装的@hapi/joi /joi

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js/tmp$ npm root -g
/usr/local/lib/node_modules

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js/tmp$ npm list -g @hapi/joi
/usr/local/lib
`-- @hapi/joi@16.1.7  

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ tree /usr/local/lib/node_modules/@hapi/joi/ -L 1
/usr/local/lib/node_modules/@hapi/joi/
├── CHANGELOG.md
├── dist
├── lib
├── LICENSE.md
├── node_modules
├── package.json
└── README.md

3 directories, 4 files

But I am not able to access it但我无法访问它

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ node
> var Joi = require('@hapi/joi'); // this is not working
{ Error: Cannot find module '@hapi/joi'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18) code: 'MODULE_NOT_FOUND' }
> 
> 
> var Joi = require('/usr/local/lib/node_modules/@hapi/joi') // This is working
undefined

My node and npm are我的nodenpm

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ which node
/usr/bin/node
alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ node -v
v10.15.2
alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ npm -v
6.11.3

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ echo $NODE_PATH # this shows nothing

alok@alok-HP-Laptop-14s-cr1xxx:~/tmp/js$ node
> process['env']['PATH']
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin'
>

Why I am getting Error: Cannot find module '@hapi/joi' ?为什么我收到Error: Cannot find module '@hapi/joi' How to troubleshoot this?如何解决这个问题?

Adding /usr/local/lib/node_modules to NODE_PATH make it work/usr/local/lib/node_modulesNODE_PATH使其工作

# Add this line in yuor ~/.bashrc
export NODE_PATH="/usr/local/lib/node_modules:$NODE_PATH"

or或者

# Add this line in yuor ~/.bashrc
export NODE_PATH="`npm root -g`:$NODE_PATH"

or或者

// Add this line at top in your node.js script
module.paths.push('/usr/local/lib/node_modules/')

Tested on Ubuntu 20.04, This should work in other Linux distributions also.Ubuntu 20.04 上测试,这也应该在其他Linux 发行版中工作。

Have you checked your user folder?你检查过你的用户文件夹吗? I have a node_module folder here /Users/<username>/node_modules It might be in there.我在这里有一个node_module文件夹/Users/<username>/node_modules它可能在那里。

And you would have to change the $PATH maybe.而且您可能必须更改$PATH

you should have to use npm init it will make package.json then you can run you js file.你应该必须使用 npm init 它将使 package.json 然后你可以运行你的 js 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM