can I bundle node_modules and do npm install from the bundle? - node.js

I am trying to setup Jenkins to do tsc but the build machine will not have internet access. To facilitate this I need to bundle the node_modules from another box with internet access and copy it over to the build machine and use this bundle to do npm install. In effect, I am trying to replicate maven's local repository structure to prevent npm install from accessing npm's repository. Is there a way to achieve this? From what I saw, npm pack facilitates deployment but not what I am trying to achieve, I need node_modules so I can compile on the build machine. Also, it is a windows machine so no luck with offline-npm.

Related

How to create an NPM environment for an offline build

I am trying to do an NPM build, install and bundle on a server which does not have a network connection
I can run the build successfully on an online server, and I can copy the directories which are required over to the offline server
How can I reproduce the NPM environment on the offline server so that an an NPM build, install and bundle will be successful?
I assume I should be copying the node_modules and package-lock.json and running an npm install --offline.
I can run the build successfully on an online server, and I can copy the directories which are required over to the offline server
Knowing that, on a system that has access to the internet, run npm install to create and populate the node_modules directory with all the dependencies.
Copy over the entire application directory including node_modules directory, allowing all dependencies available to the offline server.
This is the approach shared in comments: How to install npm package while offline?
If the dependencies need to be updated, these steps would need to be repeated.

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

npm install as a build step in TeamCity

I am studying a TeamCity project which has to do with a .NET application with Angular at the frontend. What it does not make sense to me is I cannot find anywhere npm install. For example:
The thing is in case I add a dependency in package.json which requires update of node_modules folder, everything works fine as far as the artifacts are concerned and Angular finds the files it needs!!
But how node_modules folder on TeamCity is updated?
Sorry, for being a little bit abstract; honestly, I cannot find npm install anywhere.
I would highly recommend looking at the TeamCity Node Plugin available at https://github.com/jonnyzzz/TeamCity.Node. The plugin, which is also available via the TeamCity Plugin repository for an integrated installer, will allow you to use NVM to install a specific version of Node as well as run NPM to install other dependencies, etc.
I hope this helps!

How to install just node modules without internet

There is a way to install npm package to a machine which doesn't have internet acccess is, using npm pack in machine with internet acces, copying it to machine without internet and running npm install <tar> in it. But npm pack, packs whole project.
But I want to manage and install the modules myself, without the opportunity for the developers to add/remove any modules. So I just want node_modules to be packaged. And then want to install it to machine without internet.
For example when developer push his/her commits to origin, I want to get node_modules from ftp etc. and codes from GitLab then go on continuous integration with this static node_modules.
How can I do that?
There is a solution to manage the modules yourself: you can store your node_modules in its own repo in which your developers will only be able to clone/get the repo and not contribute/modify it.
Hope this helped you
This can be done, please look at Installing a local module using npm? . You can FTP or whatever to get the packages and install them using npm.

Upload npm dependencies to a server

I need some help with the following situation:
At the moment I am working with Maven and Spring in order to do a web application. My team was working downloading the dependencies for different frameworks, but now I want to use npm to make the dependencies managment easier.
When I run npm install it downloads the dependencies locally, but the idea is not to upload the node_modules folder to git or the web, is to have the package.json file and download them manually in each computer.
I know that the app is compressed in a war file (I don't know when and how, when I started working here that was working like that), and when is compressed it downloads the maven dependencies (or that is what I understood). I want to make the same with the npm dependencies, the idea is to configure somewhere that it has to run npm install before compressing the whole application.
Does anyone know how to do that? I hope that you could get the idea.
Thank you!
Assuming that for war packaging Maven war plugin is used - you cannot invoke npm command from it.
Note: When using the war: goals it is assumed that the compile phase is already done. The WAR Plugin is not responsible for compiling the java sources or copying the resources.
https://maven.apache.org/plugins/maven-war-plugin/usage.html
As workaround for endpoint you can install required modules by npm install -g if your server have the access to npm repo. Otherwise you need to copy node-modules manually.
Don't forget about the permissions.

Resources