Yarn install package from npm and not workspace - node.js

I'm encountering a problem with yarn workspaces, here is my situation :
I have a monorepo that contains :
a packages folder containing npm packages
an apps folder containing nodejs apps
In one of my apps, I'm trying to install a package from my packages folder that is published in npm, but when I do yarn install, it keeps using the workspace folder and doesn't download the npm package nor updates the yarn lock.
How can I tell yarn to not use the workspace folder for install but download it from the remote? I'm using the workspaces only for local builds.

If you are using yarn berry, it looks up the package in your workspace first and remote registry after. This behavior can be changed by setting enableTransparentWorkspaces: false in your .yarnrc.yml file at root.
// .yarnrc.yml
enableTransparentWorkspaces: false
...
If you want to get a package from your workspace with the above option is set, you should explicitly add workspace: protocol as a prefix for your package name in your package.json.
"my-package": "workspace:^2.0.17",
See more at:
https://yarnpkg.com/configuration/yarnrc#enableTransparentWorkspaces
https://yarnpkg.com/features/protocols

if it is yarn 2+ (berry) you can use npm protocol
but I have some issue with it

Related

how to use npm to install all node modules in local network?

My node app will deploy in a bank that can't access the internet. Can I download all the node modules in a local directory, and install them from it, or just copy the node_modules to the bank machines? Any safe and practical advise will be highly appreciated, thank you.
OR
How to add node_modules dependencies without using internet .
Add offline-npm to your project to serve a npm compatible tgz file wich contains all dependencies for offline installation with npm install.
Additionally you can use offline-npm -n to install packages from your local npm cache directory
For installation
npm install -g offline-npm
Usage
1) Open terminal and go to your project you want to prepare for offline use. This folder needs to contain a package.json file.
2) Prepare your project for offline use
offline-npm --add
3) This changes the package.json file and adds a offline folder which will contain all your dependencies.
Pack your project
npm pack
For more details , visit the Link
Hope this answer gets useful to you .
Typically you will want to bundle your node_modules into your deployable artifact.
There are some gotchas here around any native dependencies as you need to match the nodejs version of the build environment with the target system.
The easiest way to achieve this in my experience is by using docker to build and package your deployment. Though it is possible to do when running directly on host machines, you may find it safest to just avoid the usage of native dependencies to remove any risk of things breaking from a nodejs or os update.
I've also successfully achieved this packaging the nodejs binary into my deployment artifact that was deployed directly on centos hosts, however we had a mixture of centos 6 and centos 7 hosts at the time, which brought additional complexity related to different glibc versions causing nodejs to fail to start with the system provided library.
In short if you can I would use docker to package your application into a completely self contained image.
you can just copy the node_modules into the deployment machine but it is bad practice.
there are other solutions like using local-npm package... your npm installs are fetched from the registry and then modules and their dependencies get stored in a local PouchDB database. This caches them so subsequent npm installs use the local cache rather than calling to the network. local-npm also takes care of keeping modules updated when they change. It does this by listening for changes to the remote registry so you don't have to worry about staleness.
or bundling your packages to use them offline you could visit this link for more details for offline installation of npm packages:
https://addyosmani.com/blog/using-npm-offline/
How to install node modules in local network?
1. Solution: yarn offline and 'Offline Mirro'
One of the main advantages of Yarn is that it can install node_modules from files located in file system. We call it “Offline Mirror” because it mirrors the files downloaded from registry during the first build and stores them locally for future builds.
2. New problem: How to use yarn in local network?
Download yarn.tar.gz into the local repository, and install it in local node_modules directory.
npm install yarn.tar.gz --no-save
3. Usage
# run yarn install, and download the node modules (.tar.gz) into the offline mirror directory '$REPOSITORY/yarn/yarn-offline-mirror'.
npm run online-install
# with yarn.lock file, install node_modules from offline mirror directory '$REPOSITORY/yarn/yarn-offline-mirror'
npm run offline-install
4. Problems
4.1. problem 1
4.1.1. description
error can`t make a request in offline mode("http://....")
4.1.2. reason
the indirect dependencies could not be downloaded into offline mirror directory
4.1.3. resolution
yarn config set yarn-offline-mirror-pruning false
5. Github demo
yarn-offline-deploy-demo
6. Reference
1. Running Yarn offline

Pack and install Node package in offline mode with yarn

I try to add a package (not published) in my project in offline mode.
I configured the yarn-offline-mirror in the yarnrc of my project :
yarn-offline-mirror "./yarn-offline-mirror"
I modify manually the package.json :
"dependencies": {
"#xxx/yyy": "1.0.0",
}
I packed my package :
yarn pack --filename #xxx/yyy#1.0.0
I moved manually the package #xxx/yyy#1.0.0 in the folder "yarn-offline-mirror" of my project.
I run in my project :
yarn install --offline
I obtain :
error Couldn't find any versions for "#xxx/yyy"
that matches "1.0.0" in our cache (possible versions are
""). This is usually caused by a missing entry in the
lockfile, running Yarn without the --offline flag may help fix this
issue.
If I publish and install my package #xxx/yyy#1.0.0 in my project (in online mode) and after, I reinstall in offline mode with yarn-offline-mirror, it works.
But I want pack and add my package #xxx/yyy#1.0.0 in my project without publish my package (in the case where I have no internet connection).
I am pretty sure you have followed the blog which contains all the steps.
I followed it as well, there is an important point it is not obvious.
The yarn-offline-mirro is based on yarn.lock file (which is auto-generated after you run yarn install). That is key relevant point in the procedure and was not well highlighted in the blog post.
Install your package as whatever other package in "online" mode with yarn install and you will see the packages .tgz are in your [off-line-folder], then you can delete [node_modules] in your project folder eventually you can execute yarn cache clean or even move you offline, but NEVER delete the yarn.lock.
After that, yarn install --offline should work as expected.

Is there anything that can keep track of my installed npm packages and restore them on a different dev environment ? (like "Restore NuGet packages")

So I'm new to node/npm packages. Here's the scenario.
I installed all the dependencies on an environment.
Pushed the code to a remote repo
Pulled the code to a different dev environment.
Tried to restore all the previously installed dependencies
automatically but couldn't figure out a way.
I worked with NuGet packages before. I was expecting something similar to the behavior of "Restore NuGet Packages".
I've looked into my packages.json file but they don't list the later installed dependencies like packages.config file would do in case of NuGet. So is there anything similar? or am a missing something?
The package.json file is doing what you are looking for. Installing a dependency with --save option will add it to the package.json dependency list. On another environment you can install same dependencies with running npm install command.
Install a dependency for the first time: npm install newpackage --save
Push the package.json to the repo
Pull the repo
Install all dependencies which listed in package.json file by running npm install command in the directory which package.json reside

Can I copy the remote folder of node mudules on git lab?

The node module that I need has been updated, and there is an old version of it installed on the gitlab project. Is it possible to get the folder for that specific module as it is when it was installed?
Thank you
You dont't want to keep source code of external dependencies in your repo. You should add your node_modules to .gitignore and remove it from the repo. Instead, you just track your dependencies in your package.json file by installing them via npm install --save. Then, when someone checks out your project, he just runs npm install and all dependencies get resolved automatically. If you want to update any of the dependencies, you update it via npm cli, and commit the updated package.json to your repo.

node_modules folder in an ASP.NET Core MVC project

I've recently upgraded my netcoreapp1.0 to netcoreapp1.1. This left me wit ha few surprises and error that needed to be fixed. On top of that, I recently got a new PC, which forced me to go through even more things.
First up I had to install NodeJS again, and from the node package manager, install bower. After installing both NodeJS and bower, and several hours after working on my project, I realized, as I had to commit the changes to remote source control, that a node_modules folder had appeared (though hidden) in my project. Git of course wants to commit this folder, but I'd rather not since it contains loads of subfolders and other items.
I was wondering what to do with this folder. I believe it shouldn't be there? And I'm not sure why it is. Shouldn't it be global or something, specific from PC to PC? Or should I just add it to my .gitignore file?
Quick info: It wasn't there prior to the upgraded project, meaning that I couldn't see any node_modules folder in netcoreapp1.0 and there's no folder in my source control?
This node_modules folder wasn't there before because before it was installed globally:
npm install -g bower
the advantages of globally installing bower is that you can use the bower command through the command-line directly.
now apparently bower was installed locally, with the following command:
npm install bower
Now it was locally installed in the node_modules folder, meaning this folder appeared.
You don't need to check this folder into get and you can simple add the following rule in your .gitignore file:
node_modules
But 1 thing to remember! when you build your application with teamcity or some other CI build tool you will now need to install npm packages (just run npm install in the folder) before you publish your application, otherwise these javascript files will be missing.

Resources