I was wrong
The reason why npm install whatever deletes the node_modules/- folder is different: I thought it runs preinstall but no postinstall and I was wrong.
I runs neither of the two scripts.
The reason seems to be that npm install whatever also removes all packages not mentioned in my package.json. Funnilly, it does not install packages mentioned in my package.json but missing in node_modules. It's exactly like Michael Waddell wrote in a comment. This makes no sense to me, but....
Following this comment, I switched to absolute imports and rely on postinstall (in my own package.json) to create the link
{
"scripts": {
"preinstall": "rm node_modules/-",
"postinstall": "ln -s `pwd`/src node_modules/-",
...
},
}
It works well, unless when I really install something. With
npm install
the link gets deleted and then re-created. However, with
npm install whatever
the link gets deleted, but not re-created. Is this a bug or is this intentional and I should use some other "postinstall"?
Please note that I'm not concerned about losing my src directory. That's a different problem. My problem is the "postinstall" running only sometimes.
Update
People commented that the hooks only get run when running npm install without any arguments. I could live with that, but it's not true:
> ls -l node_modules/-
lrwxrwxrwx 1 maaartinus maaartinus 40 May 29 11:53 node_modules/- -> /home/maaartinus/work/octopus/reocto/src
> npm i whatever
foo bar baz blah
> ls -l node_modules/-
ls: cannot access 'node_modules/-': No such file or directory
This mean that preinstall runs, but postinstall did not. This sounds like a bug...
> node -v
v12.16.3
> npm -v
6.13.4
Update 2
The pre- and postinstall scripts are in my own package.json (which can be understood from the context, but I should have stated clearly).