Download npm dist package without having to install npm - javascript

Is there a way I can download an npm package without having to do a npm view XXX ... or basically just not having to install node/npm? I'm trying to do this on a linux machine.
~EDIT~
I realize I should've added some clarification on what I'm trying to achieve here: all I need are the package's files with dependencies to be served as a static resource on cloudfront. I'm hoping npm provides a way to directly download the artefact like the way maven-central does.

You can access to the raw package using the NPM api.
https://registry.npmjs.org/{package-name}/{version}
The url above will give you a JSON response containing the package metadata. The package property dist.tarball contains an url where the package can be downloaded.
Keep in mind you have to install the dependencies of the package on your own
Example:
https://registry.npmjs.org/lodash/4.17.10
NPM API documentation

Download lib folder from github.com-get all files within it
Put all the files next to your node application in the same folder.
at the top of your file append it like sooo....
var "" = require("./""")

Related

Making a local Javascript library - Do I have to publish an NPM package?

I have some javascript utility functions that I would like to be able put in a central folder and reference from different projects.
It seems I cant import functions from outside the src file in of my project.
Do I have to publish an NPM package?
Do I have to duplicate the code in each project?
Am using javascript/node + vscode.
thanks
To create a local (unpublished) library package
Create a 'my-library' folder. Include source code, exporting any desired functions. Folder must include the 'package.json' file generated by npm init
cd into the folder of the project that needs to use your library. Run npm install --save local/path/to/my-library.
The --save will add the package to your dependencies in the project's package.json file, as it does with 3rd party published packages. It will also add a copy of the source code to the node modules folder of the project, as always.
import/require the package as you would normally, from any project. For example
import { myFunction } from "my-library"
You can use Lerna for dividing projects into multiple packages locally. It uses the same node_modules and can be deployed to NPM anytime.

Should I upload "node_modules"?

I am making a web page, node_modules file is around 150 megabytes, should I upload it or not? Is there any way to make it smaller? I am using "filezilla" and it would take too long to upload it.
Node modules is where all the external libraries you use for your application are kept. The list of those libraries should be mentioned in package.json file.
You should, typically, not upload node modules folder manually. They are the external libraries and are easily available to install separately. So, when moving files through filezilla, move everything but node modules. Then, in your server, simple run npm i before running the application.
If you have a package.json file and used npm module -s <package_name> (with -s or --save) then everything was fine.
If you don't have it no worries.Transfer the files into your online services like AWS,Something like that.
Then give the commands.
// For install npm
npm i
(or)
npm install
// To start your server
npm start
Whatever you put in your package.json file Start Object the file will be triggered.
No need to copy the node_modules folder at all.

Install a library by using npm on a server

when i want to use some library, such as Croppie, i should install it by using npm or Bower:
npm install croppie
bower install croppie
As i am using a server, i dont know where is should install it. On the server? in the wordpress files? or in the same folder that contains JS file for this library.
Thanks a lot
Basically you need to add script in your wordpress web app. So there are multiple ways to do so.
1) Add <script src="https://raw.githubusercontent.com/Foliotek/Croppie/master/croppie.min.js" />. It's better either you download this file in your file system or add a CDN link of Croppie.
2) If you have bower.json configured in your project you can use bower install croppie and then link it in your index.php (root file)
If you just have copied your project over to your server, you should have a package.json file, which contains all libraries you used. (Given you saved it via npm install croppie --save or npm install croppie --save-dev)
Then you can just go into your directory which contains the package.json and run npm install.
If you ever install a library which you would like to use without a project, you can install it globally with npm install libraryName -g, thus making it available on the command line.
In general, if you develop locally and deploy to a server, you should install dependencies locally, in /wp-content/themes/(your-theme)/ and don't upload them to your web server along with the rest of the site. The .bowerrc file, placed in the theme directory, should include the target directory in which bower will place the dependencies you've required:
{
"directory": "bower_components"
}
Then you'd want to bundle those dependencies in css/js etc. directories within the theme and deploy those. .gitignore the bower_components directory or just don't FTP that directory up, depending on how you do things.

How to depend on a large Database in NodeJS NPM?

NPM is not made for large files. My NPM package contains a 800MB SQLite database. I thought I could easily keep it out of the package tarball and require it via URL dependency.
But this dependency rule has to be used for a download of another NPM package tarball. So I will end with just another NPM tarball containing the 800MB database. By keeping it out of the package I would have to put it in another package.
Is this a bad usage of NPM and if yes, what is the best way to install the database file? Important is that NPM takes care of the proper installation and only installs the package if it contains the database.
If you
publish module A in npm
A depends on module B as a URL dependency
You host B yourself at a publicly accessible url
you are not abusing npm registry at all.
Only individual users that choose to use your package A will ever download B, but A is available in npm registry for all to find & make use of.

How to use NPM modules?

I'm completely new to frontend web dev with a very basic question. Once I npm install something, how do I actually use it? For example, I just did npm install bootstrap, and I would now like to be able to use the CSS and Javascript that it downloaded. I'm sure I shouldn't have to dig through the directories to find some entry point... so how do I now use bootstrap in my webpage?
Most modules on NPM are used in Node.js, for the server (backend). Node.js has a built-in function require('your-module') to make use of the module. This function is not present on the frontend in the browser. However, there are tools like browserify or webpack and probably others to make the NPM modules and the require function work in the frontend.
If you're just starting out I suggest you take a look at Bower first. With Bower (installed with NPM though) you can pull in all your frontend libraries like jQuery, Bootstrap, etc. to your project folder and you can point your script tags in your HTML to the bower_components/ directory, e.g. <script src="/bower_components/jquery/jquery.min.js"></script>. You can save a list of all libraries used with a version number in a json file called bower.json in the root of your project folder.
Based on this file you can pull in or update all the libraries listed with the use of the command line.
As a really general rule, npm is used for assets your node app will use on the server, while bower (and others) are the equivalent for dependencies that you want to use on the client.
That said, the use is basically the same.
npm (and bower) install the files into your project directory in a standard location. All you really have to do is make sure that location is accessible via a web request (typically, node_modules is not; which is why bower came about), and then embed link and script tags as appropriate in your html:
<script src='/node_modules/bootstrap/js/bootstrap.min.js'></script>

Categories

Resources