yarn - install dependencies for a package in a different folder? - node.js

I would like to install the dependencies for some_project. I know I could cd into some_project and then run yarn install
But I was wondering if it's possible without changing the directory?

There's a bug with yarn that prevents the --modules-folder option from working as intended, however I personally don't like the fact that the option has to be provided in the command line; how would you make sure that on future installs they get installed to your chosen folder?
That's why I came up with this sneaky solution for npm since I wanted full control of the install path, not just into a prefixed version of node_modules. It will work just as well with yarn, in fact we use it in production and we haven't had a problem yet (fingers crossed).
In a nutshell, you need to symlink node_modules to your desired folder in the preinstall event (to trick npm or yarn) and then delete the symlink in the postinstall event.
There's one caveats however, which I didn't mention in the linked answer (since I didn't think of it at the time) and that is, things might not go as planned if the install goes horribly wrong; you'll end up with the symlink in place since the postinstall event might not have been triggered, and then the next install might fail since the symlink already exists.

Related

Is using npm-ci by developers a good NPM working process?

I work at a largish project with ~10 devs. We have package.json and the resulting package-lock.json committed, and our ci pipeline does npm ci to restore packages according to package-lock.json.
Currently, the developers are instructed to clone the repo and run npm install. However, I found that npm install will install different versions that match the version spec in package.json - for example, ^5.0.5 might cause npm install to install version 5.1.1, or to keep 5.0.5 if it was already in there.
So, I want to change the instructions for developers to:
(common case) If you don't want to change packages or package versions, only use npm ci
If you do, use npm install and/or npm update (possibly with --save-dev), test locally, and then commit the resulting package.json and pacakge-lock.json.
Are these instructions sound? Am I missing something?
Per documentation "this command is similar to npm install, except it's meant to be used in automated environments such as test platforms, continuous integration, and deployment -- or any situation where you want to make sure you're doing a clean install of your dependencies." (emphasis mine).
I prefer using it instead of "install", because it gives some insurances about state of node_modules folder.
It will remove modules folder, if it is present, which will remove everything that is not in lock file, but may accidentally be present from previous install.
It will throw an error if someone changed dependencies by hand and didn't updated lock file.
It will be faster than install, because it doesn't need to build new dependency tree, and it will preserve versions of dependencies which were installed by tag (like latest or next) or by wild card (*). And sometimes this is a very good thing - recent colors incident is a good illustration.
Basically it means that me and all my colleagues will get identical node_modules folder contents. One of the advantages of Yarn in early days were reproducible installs with lock-file, and it is considered a good practice.

Before npm install delete node_modules?

Is it necessary to delete node_modules before executing an 'npm install' for a more reliable installation or does npm overwrite everything?
Npm definitely does not overwrite everything every time. It runs through a fairly complicated process that I won’t dive deep into here, but in general you should not need to delete node_modules every time. npm exists to handle this situation and generally will only download newly added or updated packages.
I will occasionally find myself in the need to completely remove the node_modules directory in the event that I have been add/removing/updating a large number of packages. Sometimes packages can get cached. But it’s not a common occurrence and I usually only recommend it when you notice a package appears out of date.

package-lock, package.json, woe

I'm not a node expert by any means. In one project, something's gone wrong somewhere, and package-lock.json and package.json seems to have fallen out of sync. The only way I can get stuff to build is this sequence:
rm -rf node-modules
npm install
rm package-lock.json
npm install
webpack
i.e. I have to run npm install once with package-lock, and then once without. There are a lot of dependencies, and tracking down which ones are needed is proving difficult. What's the best way of resolving this so that I don't need to npm install twice? And how can I prevent this sort of thing not arising in the future?
Note: Two different devs were working on this git repo, and it's very possible that the package-lock and package files were not checked in properly.
Could You paste Your packake.json and package-lock.json files here?
If they are too big - put them in some fillde and give link.
(srry that this is an answer, not a comment, but don't have 50 rep needed).
It turns out the issue was to do with a specific version of #types/react-redux. There's a breaking change between 4.4.40, and 4.4.41. I was previously using ^4.4.40, so when the package-lock was being used, it was fetching 4.4.40, and everything worked. When I deleted the package lock, the ^4.4.40 moniker downloaded 4.4.41, as it was latest, and things broke. Changing the version from ^4.4.40 to just 4.4.40 has fixed it for now.

npm install creates inordinate amount of dependencies in node_modules

This started happening just recently, but every time I run npm install I end up getting dozens of node modules beyond what's listed in package.json.
This answer shows that this is a new feature of npm 3 where the dependencies are being "flattened" instead of nested. However, I don't want to look at a bazillion modules every time I venture into the folder. Is there any way I can disable this setting?
No, that cannot be disabled.
https://github.com/npm/npm/issues/10079
is there anyway that I can force npm#3 to install new package for me, but old way? So without calculating project-wise tree. I just want the new package to be placed in node_modules with its dependencies in its node_modules?
No. The new installer is pretty much a complete rewrite, and while there is some special-case code to install packages into siloed subdirectories, that's only available when doing global installs, to simplify packaging and managing shared tools.

Is it safe to copy node_modules?

If I want to copy a node project: Does it make any difference if I just copy node_modules or install all the modules again from scratch via npm?
2017-05-12
I've updated this answer to reflect changes since the release of npm 3.x and new tools that are available.
npm v3 dependency installation is now non-deterministic meaning you may get different packages depending on the order in which packages have been installed over time. This isn't necessarily a bad thing, just something to be aware of.
Given this change I personally don't copy my node_modules directory around too much (it's still possible though!) and instead opt for a clean install most of the time.
There are new tools like Yarn Package Manager which can speed up the installation process if you are doing that a lot (but as of 2017-05-12 it's unclear how well it handles private npm organisations and private scoped packages).
So the takeaway is still pretty much the same: it won't hurt, but maybe err on the side of a clean install. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.
Original answer from 2014-06-08:
In general it should be fine - I copy the node_modules directory sometimes from my other projects to speed up the setup process.
You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required.
In short: it won't hurt. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.
Linux command
To copy projects without node modules directory:
In the directory of your projects run this:
rsync -av --progress --exclude="node_modules" <source directory> <target directory>
for example:
rsync -av --progress --exclude="node_modules" . /mnt/d/omer/new-directory

Resources