Skip to content
This repository has been archived by the owner on Aug 17, 2019. It is now read-only.

delete contents of node_modules but keep the dir itself #47

Closed
wants to merge 4 commits into from

Conversation

tareksha
Copy link

@tareksha tareksha commented Apr 9, 2018

Keep node_modules directory but delete its contents.

In some CI setups node_modules is a special directory, e.g. a mounted folder or a symbolic link. For these setups simply deleting node_modules won't work, or worse will work but break the CI setup. Deleting node_modules is a also a non-trivial deviation from the expected behavior of npm install which means the existing CI setups that are explained above are blocked from migrating to npm ci.

npm ci should minimize violent changes and concentrate on the fact we want to reset the installed dependencies and proceed with the regular flow. Keep node_modules untouched, doesn't matter how it was created.

Copy link
Owner

@zkat zkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable enough idea! Thanks for this patch! I've made a comment about the change itself, and I'm wondering if you could make the patch even better by writing a test that verifies this?

index.js Outdated
@@ -120,7 +121,9 @@ class Installer {
)
return BB.join(
this.checkLock(),
stat && rimraf(path.join(this.prefix, 'node_modules'))
stat && readdirAsync(path.join(this.prefix, 'node_modules')).then(children =>
BB.join(children.map(child => rimraf(path.join(this.prefix, 'node_modules', child))))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want this to be BB.all() instead, since that's the one that awaits an array of promises.

There's also an alternative implementation you can do: since rimraf supports glob out of the box, rimraf('foo/*') should delete the contents of foo without removing the directory itself. It might be slightly easier to read the code that way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion!

@surgeboris
Copy link

This should fix npm/npm#21007
I'll try to write a test for this PR once I'd be able to spare some time for that.

@surgeboris
Copy link

@tareqhs @zkat Added test, created PR into original fork: tareksha#1

Add test to make sure that node_modules/ folder is not deleted
@tareksha
Copy link
Author

thanks for the tests @surgeboris !

@mcfedr
Copy link

mcfedr commented Oct 16, 2018

Came here to make this PR, and its already here, awesome!

To add to case for this change, I'm building a docker image, where I have created the node_modules dir and given npm user access to the folder, but not the containing dir, so it crashes when trying to use npm ci when it tries to delete node_modules

RUN mkdir /srv/project/node_modules \
    && chown -R node /srv/project/node_modules

USER node
RUN npm ci

@bgaillard
Copy link

bgaillard commented Nov 6, 2018

Hi, came here just to indicate I reported the same need on our side here : https://npm.community/t/running-npm-ci-without-deleting-old-node-modules-folder-20104/3152.

In our case we cannot remove the node_modules folder because its mounted as a Docker tmpfs volume to speed-up installs (this is a thing I would advise to everybody to speed-up their installs in CI environments ;-)).

Other posts about this subject :

So I agree with this PR 👍 and wait it to be merged and integrated inside npm / cli.

Thanks,

Baptiste

@tareksha
Copy link
Author

tareksha commented Nov 6, 2018

Hi @zkat , any progress here ?

@tareksha
Copy link
Author

@zkat a gentle reminder - you are completely ignoring this PR :)

@lnikell
Copy link

lnikell commented Jan 22, 2019

Would be great to see it in npm, have issues described above, so can't use npm ci at all.

@tareksha
Copy link
Author

@lnikell the authors are completely ignoring this PR although many devs need the fix

@lnikell
Copy link

lnikell commented Jan 23, 2019

@tareqhs that's very sad, at least I would like to hear some feedback even if it "you should not use it like this, that's why script deletes the whole folder"

@kumarrishav
Copy link

Any update on this? While mounting node_modules as docker volume. npm ci is crashing saying: resource is busy while deleting node_modules folder.

@OldrichKruchna
Copy link

Hello there, any update on this?

@joewood
Copy link

joewood commented May 23, 2019

With the new vscode support for devcontainers this fix has become more urgent. Right now the examples use a full install rather than a "ci" after setting up the container: https://github.com/microsoft/vscode-remote-try-node/blob/master/.devcontainer/devcontainer.json#L8

@ErikHumphrey
Copy link

ping

@MrWako
Copy link

MrWako commented Jul 22, 2019

Out team could really do with this fix for our containerised build too please.

@pioneer32
Copy link

Any update on this?

@zkat
Copy link
Owner

zkat commented Aug 17, 2019

Please file this PR over at https://github.com/npm/libcipm, as this repo is no longer in use, and I am no longer employed at NPM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.