npm install unexpectedly removes folder from node_modules

Hi there,

This is my .gitignore file

node_modules/*
!node_modules/@my-private-namespace

In the @my-private-namespace folder I store packages shared across the project, that I can’t publish on npm.
This pattern makes easy to require one of these packages.

require("@my-private-namespace/foo");

The problem is that when I install/uninstall something using npm install/uninstall,
the folder @my-private-namespace gets removed.

Do you know anyway I can prevent this to happen?

Thank you

npm shouldn’t delete delete packages that are listed in the package dependencies or devDependencies. Are you installing your private packages with npm, or just copying them and requiring them from code?

@shadowspawn

At some point I manually added these packages in the dependencies list in the main package.json; but the result didn’t change.

Your situation will be more complicated, but it worked as expected for me in a simple test.

  1. I create a dummy package foo with:
"name": "@my-private-namespace/foo",
  1. I created a folder bar:
$ npm init -y
...
$ npm install ../foo
...
$ ls node_modules/\@my-private-namespace/
foo
$ npm install n
...
$ ls node_modules/\@my-private-namespace/
foo

Yep, this works quite well… but it’s like a workaround to the problem, cause now the package is not stored in the node_modules/@my-private-namespace.

Oh, I skipped over your mention of .gitignore. You have your package in source code control directly in the node_modules folder? I don’t think this a supported or safe layout. I think of npm as owning the node_modules folder. I tell npm what packages I want, and it arranges them how it likes, removing duplicates, adding meta-data, adding links into .bin, hoisting packages so they can be shared, etc.

I’ll speculate on some other possible setups.

You could link the packages in. The downside is it requires running some extra commands during setup. Links are a local development mechanism and not a publishable setup.

Another approach is putting each package in a separate repo and installing them using a git URL.

If your high level goal is just easy requires of shared code, there may be other approaches too. Mentioning that in case the packages are not actually needed as such.

Thanks for the help.
It would be useful, imho, to have the possibility to tell npm: “ok, do whatever you want in node_modules, butt please do not touch node_moduels/@my-local-stuf”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.