Fix module_not_found nodejs installed via nvm on Ubuntu 20.04.md
So you have installed a global module successfully with npm i -g <package>.
However when you require it via require('<package>'), it would complain with error code: 'MODULE_NOT_FOUND'.
Here is a fix that worked for me.
- Check whether it is true that the required package is installed globally.
npm list -g --depth=0
This command will show which packages are installed globally.
- Now check these which paths:
1 2 3 4 5 6 | |
The echo $NODE_PATHdoes show an empty line on my terminal.
Append the node_modules to the path returned by which node as follow:
from
/home/user/.nvm/versions/node/v12.18.3/bin/node
to
/home/user/.nvm/versions/node/v12.18.3/lib/node_modules
- Now from your terminal, you can run
export NODE_PATH=/home/user/.nvm/versions/node/v12.18.3/lib/node_modules
Bonus:
To apply this every time you open your terminal, you can create and add the above export command to
/etc/profile.d/profile.shOr append the export command to
~/.bashrcRefresh/reload:
source ~/.bashrcCheck with one is working for you. Type
env