Install a library by using npm on a server - javascript

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.

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.

Download npm dist package without having to install npm

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("./""")

Use javascript file in a different project

I have a javascript (which uses files from it's own project) file that I need to use in a different project without copy the code.
How can I do it?
Thanks
Well... You could install Bower, upload your file to your own repository or gist on GitHub and then use bower to include them as a dependancy for your project.
Setting up (assuming you have Node.js already installed)
npm install -g bower
cd /path/to/project-A
bower init
bower install <link to your gist/repo> --save
cd /path/to/project-B
bower init
bower install <link to your gist/repo> --save
And then in the future, if you upload changes to your repo/gist (and the link has not changed...)
cd /path/to/project
bower update
EDIT: That or you have a remotely hosted version of this file that you directly link to, I guess...? Kinda sucks though, could wreck a live project from a dodgy edit that way

How can I get bower_rails to install my Bower assets on Heroku?

I want to use bower_rails to incorporate bower assets into my Rails application. I don't want to commit these assets into my repo though, I just want to specify them in my Bowerfile and have them installed by Heroku during deployment.
How can I do that?

Categories

Resources