configure custom module for Node.js project - node.js

I create my Node project (npm init). In this project, i want to use Node REST Client from git hub https://github.com/dchester/node-rest-client.
update package.json with dependency:
...
"dependencies": {
"rest-client": "*"
},
...
create dir node_modules in my project
Copy the rest-client into the directory 'node_modules'
Run 'npm install' to install this dependency, however it doesn't.
What am i doing wrong? how to properly install dependency that is not from npm central repo?
Thank You

It looks like you'll have to do it manually for every module. However as the accepted answer in How to install a private NPM module without my own registry? by Mihai points out, npm knows about git:
npm install git://github.com/visionmedia/express.git
furthermore, besides going into the directory and typing npm install there it is possible to specify the path as argument:
npm install path/to/somedir
There's another useful answer in How to install a private NPM module without my own registry? where you can see you can also specify a url pointing to a tarball. I suggest you take a look at that thread.

Related

Npm Install straight from package.json

I have a simple question that i cant seem to find the answer for. I cloned a git repo to my local machine.
When attempting to start node, I receive an error because i don't have the required npm dependencies installed. However, they are located in the packages.json file that was cloned.
I was wondering if there was a simple way to install the dependencies located in that file, without having to npm install for every individual package.
Within the directory of the package.json file, just run npm install. It will read package.json and install all dependencies. If you want to limit it to only non-dev dependencies, use npm install --only=production.

npm install fails because package is missing in registry

I have an issue with a project where we are using node and brunch. The issue is current specific to brunch, but could occur for any module would be my guess.
The easiest way to currently reproduce this, is to do the following in a new folder:
npm init
npm install --save-dev brunch
The issue here is that brunch depends on loggy, which in turn depends on ansi-color, which no longer has an entry in the npmregistry:
https://registry.npmjs.org/ansi-color
I think this might be the github project: https://github.com/loopj/commonjs-ansi-color
In any case, I am unable to proceed, and all our builds fail because they are not able to fetch the given dependency.
I could perhaps use npm shrinkwrap in some way, but that depends on the modules already existing in node_modules, which I am currently missing.
So how can I force npm to use ansi-color from a different location, or ignore the dependency?
Not sure about npm 2 but you can fix this with beta npm 3. npm 3 has flat node_modules directory. So sub modules can sit in the top level. Read the Changelog.
The missing modules can be installed directly from their Github repo as a toplevel dependency in your project. If npm finds the module with the same version in node_modules directory, it won't look for it anymore in the registry.
Install npm 3:
npm install -g npm#3-latest
Then install depencies:
//install missing module from other location
npm install https://github.com/loopj/commonjs-ansi-color.git --save-dev
npm install --save-dev brunch
It looks like ansi-color is back on the npm registry ("https://registry.npmjs.org/ansi-color" is back online)

How to install node.js modules downloaded manually [duplicate]

There are quite a few modules which are listed on node's github page but are not published with the npm-registry. These modules can't be installed using npm.
What is the correct way to install these nodejs modules after cloning them from Git?
You need to download their source from the github. Find the main file and then include it in your main file.
An example of this can be found here > How to manually install a node.js module?
Usually you need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it in your application folder and append this on the top of your main js file.
var moduleName = require("path/to/example.js")
These modules can't be installed using npm.
Actually you can install a module by specifying instead of a name a local path. As long as the repository has a valid package.json file it should work.
Type npm -l and a pretty help will appear like so :
CLI:
...
install npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <pkg>
npm install <pkg>#<tag>
npm install <pkg>#<version>
npm install <pkg>#<version range>
Can specify one or more: npm install ./foo.tgz bar#stable /some/folder
If no argument is supplied and ./npm-shrinkwrap.json is
present, installs dependencies specified in the shrinkwrap.
Otherwise, installs dependencies from ./package.json.
What caught my eyes was: npm install <folder>
In my case I had trouble with mrt module so I did this (in a temporary directory)
Clone the repo
git clone https://github.com/oortcloud/meteorite.git
And I install it globally with:
npm install -g ./meteorite
Tip:
One can also install in the same manner the repo to a local npm project with:
npm install ../meteorite
And also one can create a link to the repo, in case a patch in development is needed:
npm link ../meteorite
Edit:
Nowadays npm supports also github and git repositories (see https://docs.npmjs.com/cli/v6/commands/npm-install), as a shorthand you can run :
npm i github.com:some-user/some-repo
Download the code from github into the node_modules directory
var moduleName = require("<name of directory>")
that should do it.
if the module has dependancies and has a package.json, open the module and enter npm install.
Hope this helps
You can clone the module directly in to your local project.
Start terminal. cd in to your project and then:
npm install https://github.com/repo/npm_module.git --save
Step-by-step:
let's say you are working on a project use-gulp which
uses(requires) node_modules like gulp and gulp-util.
Now you want to make some modifications to gulp-util lib and test it locally with your use-gulp project...
Fork gulp-util project on github\bitbucket etc.
Switch to your project: cd use-gulp/node_modules
Clone gulp-util as gulp-util-dev : git clone https://.../gulp-util.git gulp-util-dev
Run npm install to ensure dependencies of gulp-util-dev are available.
Now you have a mirror of gulp-util as gulp-util-dev. In your use-gulp project, you can now replace: require('gulp-util')...; call with : require('gulp-util-dev') to test your changes you made to gulp-util-dev

npm install <git> with dev dependencies

A typical approach to handle private npm modules is to put them to a Git repository, and then use npm install with the path to that repository to install the module.
If you enter the dependency into your package.json file, you can even install using npm install without the need to specify the repository url every time. I.e., if you add
"myPrivateModule": "git+ssh://git#github.com:myGitHubAccount/myPrivateModule.git"
as a dependency, you can install using
$ npm install myPrivateModule
and everything works fine :-).
Now I have a problem in that myPrivateModule is private, yes, but not a dependency. Instead, it's a dependency only for development time, hence I put it into the section devDependencies in the package.json file.
Once you do this,
$ npm install myPrivateModule
does not work any longer, as it now searches the public registry instead of using the registered path to the repository.
Is there a possibility to make this work, without the need to specify the full-blown path each and every time?
Just npm install with no arguments should read package.json and install it.

How to install a node.js module without using npm?

There are quite a few modules which are listed on node's github page but are not published with the npm-registry. These modules can't be installed using npm.
What is the correct way to install these nodejs modules after cloning them from Git?
You need to download their source from the github. Find the main file and then include it in your main file.
An example of this can be found here > How to manually install a node.js module?
Usually you need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it in your application folder and append this on the top of your main js file.
var moduleName = require("path/to/example.js")
These modules can't be installed using npm.
Actually you can install a module by specifying instead of a name a local path. As long as the repository has a valid package.json file it should work.
Type npm -l and a pretty help will appear like so :
CLI:
...
install npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <pkg>
npm install <pkg>#<tag>
npm install <pkg>#<version>
npm install <pkg>#<version range>
Can specify one or more: npm install ./foo.tgz bar#stable /some/folder
If no argument is supplied and ./npm-shrinkwrap.json is
present, installs dependencies specified in the shrinkwrap.
Otherwise, installs dependencies from ./package.json.
What caught my eyes was: npm install <folder>
In my case I had trouble with mrt module so I did this (in a temporary directory)
Clone the repo
git clone https://github.com/oortcloud/meteorite.git
And I install it globally with:
npm install -g ./meteorite
Tip:
One can also install in the same manner the repo to a local npm project with:
npm install ../meteorite
And also one can create a link to the repo, in case a patch in development is needed:
npm link ../meteorite
Edit:
Nowadays npm supports also github and git repositories (see https://docs.npmjs.com/cli/v6/commands/npm-install), as a shorthand you can run :
npm i github.com:some-user/some-repo
Download the code from github into the node_modules directory
var moduleName = require("<name of directory>")
that should do it.
if the module has dependancies and has a package.json, open the module and enter npm install.
Hope this helps
You can clone the module directly in to your local project.
Start terminal. cd in to your project and then:
npm install https://github.com/repo/npm_module.git --save
Step-by-step:
let's say you are working on a project use-gulp which
uses(requires) node_modules like gulp and gulp-util.
Now you want to make some modifications to gulp-util lib and test it locally with your use-gulp project...
Fork gulp-util project on github\bitbucket etc.
Switch to your project: cd use-gulp/node_modules
Clone gulp-util as gulp-util-dev : git clone https://.../gulp-util.git gulp-util-dev
Run npm install to ensure dependencies of gulp-util-dev are available.
Now you have a mirror of gulp-util as gulp-util-dev. In your use-gulp project, you can now replace: require('gulp-util')...; call with : require('gulp-util-dev') to test your changes you made to gulp-util-dev

Resources