Pack and install Node package in offline mode with yarn - node.js

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.

Related

Run `yarn remove <dependency_name>` to remove dependency, but yarn.lock still shows the removed dependency

In my node.js project, I had using yarn installed the dependency #nestjs/jwt, now I want to uninstall it since I am not using it.
I run yarn remove #nestjs/jwt. It was successful. I checked my package.json, it was removed. But when I check the yarn.lock file, it is still showing. Why is that?
My git add -p yarn.lock shows me:
-"#nestjs/jwt#8.0.0", "#nestjs/jwt#^8.0.0":
+"#nestjs/jwt#^8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/#nestjs/jwt/-/jwt-8.0.0.tgz#6c811c17634252dd1qcd5dabf409db4692b812da"
integrity sha512-fz2LQgYY2zmuD8S+8UE215anwKyXlnB/1FwJMLVR47clNfMeFMK8WCxmn6xd0hF5JKuV1crO6FVabb1qWzDxqQ==
Besides packages you explicitly install, packages depend on other packages. To see a graph of any dependents of this package you have installed, do:
yarn why #nestjs/jwt -R
Yarn.lock is what yarn uses to know what versions of each dependency are installed so it can get those exact versions again when you run yarn install on a new machine. Try running 'yarn upgrade'. This should create a new yarn.lock file without those dependencies.

Yarn install package from npm and not workspace

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

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

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.

How to sync `yarn.lock` with `package.json`?

I installed a package with yarn add --dev, run its setup process and during it, the package installed several other packages and added those to package.json (in devDependencies), I assume with npm. Great, but now my yarn.lock is out of sync.
What is the correct, non-manual way of syncing yarn.lock to the current state of package.json?
Edit: yarn check shows the missing packages as:
error Lockfile does not contain pattern: <package>#<version>
But it doesn't add them.
Run yarn install, or just yarn.
The lock file is updated in its entirety on any change to dependencies, i.e. when you run a yarn command.
From the Yarn docs:
Your yarn.lock file is auto-generated and should be handled entirely by Yarn. As you add/upgrade/remove dependencies with the Yarn CLI, it will automatically update your yarn.lock file. Do not edit this file directly as it is easy to break something.
(Emphasis my own)
If you ever face a checksum issue this will solve it,
YARN_CHECKSUM_BEHAVIOR=update yarn

Resources