npm local prefix always set to current working directory

Viewed 26

I've been searching for a while to try to find the answer to this. I've tried everything I've found that makes sense and more. Nothing has worked. Here's the breakdown:

  • I'm on linux (12.3-RELEASE FreeBSD 12.3-RELEASE r371270 NFSN64 amd64)
  • I've installed a package called hexo-cli using npm.
  • My home directory is /home/private
  • My global npm install directory is /home/private/.npm-global
  • My npm prefix -g location is: /home/private/.npm-global
  • When my cwd is /home/public/ and I execute hexo, hexo runs
  • hexo then fails to find itself because it's using the npm prefix (this is my guess)
  • When I type npm prefix it always returns my cwd
  • I have checked /home/private/.npmrc file. A grep "prefix" /home/private/.npmrc returns:
  • prefix=/home/private/.npm-global
  • There are no other lines or entries returned from the grep
  • npm config set prefix=/home/private/.npm-global only modifies the global prefix, not my local prefix

How do I get my local prefix var to be /home/private/.npm-global?

Edit:

Here is the output of my terminal when running hexo:

[username /home/public]$ hexo
ERROR Cannot find module 'hexo' from '/home/public'
ERROR Local hexo loading failed in /home/public
ERROR Try running: 'rm -rf node_modules && npm install --force'

Edit #2:

Forgot to mention. I also tried adding the following two lines to my ~/.profile file:

export PATH=~/.npm-global/bin:$PATH
export PATH=~/.npm-global/lib/node_modules:$PATH

I would then source ~/.profile.

This made absolutely no difference.

Edit #3:

Fixed a path in the 6th bullet point.

Edit #4

This is the output of my npm config list:

[username /home/public]$ npm config list
; "user" config from /home/private/.npmrc

prefix = "/home/private/.npm-global"

; node bin location = /usr/local/bin/node
; node version = v16.16.0
; npm local prefix = /home/public
; npm version = 8.13.0
; cwd = /home/public
; HOME = /home/private/
; Run `npm config ls -l` to show all defaults.

Notice it says npm local prefix. Attempting to npm config set local prefix ... does not work. local prefix is not a variable you can assign a value to. I tried localprefix as well. This did not work.

1 Answers

I don't know what the problem was. However I've nuked everything from orbit and started over. I deleted everything except a single html file in my /home/public directory. I deleted /home/private/.npm-global. I recreated /home/private/.npm-global. I followed the steps listed in the answer to:

Global Node modules not installing correctly. Command not found

Specifically the steps provided by Vicente, posted Feb 9, 2019 at 16:30. Which were:

[username /home/public]$ mkdir /home/private/.npm-global
[username /home/public]$ npm config set prefix '~/.npm-global'
[username /home/public]$ export PATH=~/.npm-global/bin:$PATH
[username /home/public]$ source ~/.profile
[username /home/public]$ npm install -g hexo-cli

Then I tried to run from /home/public: hexo init blog

This command worked and it creates a blog directory in the cwd. However, then I tried to run hexo server from the same directory I was still in. This did not work. I changed directories to /home/public/blog/ and then typed hexo server. This still did not work and would only print out some help message about a few commands you could run.

Their website says this is generally due to a missing version line in the dependency of the blog/package.json file. I edited the file and added version 3.2.2, and then re-ran it as hexo --version. When I did this, it dumped out some Validating config output with a bunch of dependencies and their version numbers. The version number specified for hexo was hexo: 6.3.0. I went to open the blog/package.json file again to update the version from 3.2.2 to 6.3.0. However, hexo had already updated the file for me.

Then I typed hexo server while still in /home/public/blog and then the hexo server started up. It errored out with some NaN is not a valid date! error, but the server was still running.

In a separate terminal connected to the same machine and inside the /home/public/blog directory I typed hexo new "Hello Hexo". This worked. Then I typed hexo generate to generate the static files. This also worked.

Everything seems to be working now. Not sure what the problem was earlier. Would still like to know how to set the local prefix to a specific path though, if possible.

Related