[BUG] npm deleting contents of local directory #6427
Labels
Bug
thing that needs fixing
Needs Triage
needs review for next steps
Release 9.x
work is associated with a specific npm 9 release
Is there an existing issue for this?
This issue exists in the latest npm version
Current Behavior
Note: my issue is similar to #4863, but my situation is somewhat different (with similar symptoms), so I am filing this to document my case.
I have a
package.json
with the following contents:My directory structure is like this:
So I store my app's local modules (not publicly published in any way) in
root/modules
. But for convenience withrequire()
/import
, I make a symlink whereroot/node_modules/@local/modules
-->root/modules
.This lets me do
const a = require("@local/modules/a.js")
in my code.All this works fine. And with the
"@local/modules": "file:./modules"
line in mypackage.json
,npm
leaves this directory alone and doesn't empty it out when I'm installing/upgrading other dependency packages.So then, for even more convenience in my code, I decide to add another symlink:
root/node_modules/@
-->root/node_modules/@local/modules
. That means I can now doconst a = require("@/a.js")
, which is so much nicer.But now, whenever I do an
npm install some-package
, the CLI tool goes into@/
directory and completely empties it (but leaves it in place). Argh!Luckily, I had backups of those local modules so I didn't lose them.
I really expected that since the files (
a.js
andb.js
) are covered by a listing in mydependencies
, thennpm
would leave them alone. But since it finds them twice, once as@/a.js
and once as@local/modules/a.js
, I guess it's not smart enough to correlate.So I was thinking maybe
npm
needs me to also list my special@
symlink as a dependency similar to how I listed@local/modules
with thefile:..
protocol.Unfortunately,
npm
chokes whenever I add a line to my dependencies like:"@": "file:./modules"
. It says that@
is invalid dependency name. WHAT!? WHY!?Anyway, then I decided maybe I could remove the
@
symlink right before installing a package dependency, then restore it right after. Seemed like a job forpreinstall
/postinstall
.But alas, those scripts don't get run when you do
npm install something
, only when you runnpm install
by itself.So I'm stuck here. How do I get npm to leave my local files alone?
Expected Behavior
I want whatever listing I add to my
package.json
to tell npm to leave the contents of my local modules directory alone.Steps To Reproduce
(see above)
Environment
The text was updated successfully, but these errors were encountered: