Make app installable from both GitHub & NPM - node.js

require('./module') VS require('module')
I want my app be installable from both sources GitHub and NPM. I am struggling with require directives and folder structures. NPM installs modules in node_modules but git would clone into newly created directory inside the working directory and I have to require with ./mymodule syntax.
I have about 10 modules used by my app, each of them has it's own repository.
How to solve this problem? How to organize folders on the development machine? How to organize repositories?

You just have to clone your repositories in the node_modules folder!
If you want to automatically add your repos via npm install, you can add your them to your depencies in your package.json as specified on the docs:
"dependencies" : {
"project": "git://github.com/user/project.git#commit-ish"
}
And if you want to include GitHub projects, you'll just have to refer to GitHub urls as just "foo": "user/foo-project":
"dependencies": {
"express": "visionmedia/express",
"mocha": "visionmedia/mocha#4727d357ea"
}

Related

NPM Install Same Package / Different source as devDependency

Is it possible to get NPM to install a dependency from its repository or a custom repo depending on if its meant to install production dependencies only, i.e. --production. and install as a symlink when installing dev dependencies? I want developers to be able to run and debug from local source while production pulls from the github repo. I'm also afraid if the package.json only indicates the repo as a source, a developer will change the package.json to debug and forget to change it back when committing.
I have my package.json like this but with this it will only install the package when I allow dev dependencies and installs nothing when I don't
"dependencies": {
"myPackage": "github:mygithub/mypackage",
},
"devDependencies": {
"myPackage": "file:./../mypackage"
}

How to install a particular package through package.json in node directory into a different directory?

here is sample package.json file
dependencies {
"inversify": "^5.0.1"
}
I would like to install the package into different directory inversify_backup under node_modules . Is there any option to do it
Is this your folder structure?
node_modules
inversify_backup
Technically you could cd node_modules/inversify_backup then run npm i <package-name>.
Although your dependency install would be overwritten when running in production when the build job runs the npm install command.
I would recommend forking the inversify repository and just including it in your code if you need to make changes to the library.

how to Add my node_module, modules into package.json

I have some module in my node_module folder but because I am amateur in nodejs, when I wanted to install theme, I forgot to use --save with npm install.now I have lots of module but my package.json is empty so is there any way to add theme into package.json.
Sorry if my question is silly one I am beginner in nodejs
Simply change into the directory containing node_modules, backup any existing package.json in there, then use npm init to re-create the package.json.
The generated package.json will include any modules that already exist within node_modules.
Sample run:
$ cd /my/project
$ mv package.json package.json.bak # Backup package.json
$ npm init # Recreate package.json with dependencies populated
Already asked and well answered!
Here're different ways suggested to create / maintain package.json file
Is there a way to automatically build the package.json file for Node.js projects
Its simple. Edit the package.json file and add the following for development dependencies:
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"broccoli-merge-trees": "^0.2.1",
"broccoli-svg-sprite": "^1.0.3",
......
}
To get a list of package names and version numbers, you may look at node_modules/module folder/package.json for each of the modules to pick up the official package name and version. It will be of the form:
{
"name": "<<name of the package>>",
"version": "2.1.0",
"description": "broccoli asset revisions (fingerprint)",
....
}
just copy the name and version information from above into devDependencies into your project's package.json and you should be good to go.
Also have a look here Is there a way to automatically build the package.json file for Node.js projects
and here: https://docs.npmjs.com/files/package.json
You can install the same package again using npm install --save <package> and it should just replace the current package files with freshly installed ones. It will also add the packages you already added with the default version notation.

npm install not installing latest version on GitHub

I have a module called 'sails-mongo' and I want to update it to the newest version using the following command:
npm update sails-mongo --save
I also tried uninstall then install again. I tried sails-mongo#latest and sails-mongo#beta.
Problem:
The current version (master) on GitHub the package.json (https://github.com/balderdashy/sails-mongo/blob/master/package.json) file has:
"dependencies": {
"async": "~0.2.9",
"lodash": "~2.4.1",
"mongodb": "1.4.2",
"waterline-errors": "~0.10.0"
},
And in the one being updated
"dependencies": {
"async": "0.2.10",
"underscore": "1.5.2",
"underscore.string": "2.3.3",
"mongodb": "~1.3.23"
},
The only way I get the master branch is using the command npm install git+https://github.com/balderdashy/sails-mongo
Why doesn't sails-mongo#latest install the master branch?
By default, NPM dependencies are pulled from the NPM repository. Authors must manually upload new versions of their software to the NPM repository, so the "#latest" version of the code hosted on NPM is different from the latest version of the code that exists anywhere (e.g., on GitHub).
According to the NPM repository's info page on Sails, the latest NPM-hosted version is 0.9.16 while the current GitHub version is 0.10.0-rc3.
If you want to have your project depend upon a particular branch or commit of a particular Git repo (instead of the version(s) hosted on the NPM repository), the NPM developers have included an explicit mechanism to allow this, detailed in "Git URLs as Dependencies" in the package.json docs:
Git URLs as Dependencies
Git urls can be of the form:
git://github.com/user/project.git#commit-ish
git+ssh://user#hostname:project.git#commit-ish
git+ssh://user#hostname/project.git#commit-ish
git+http://user#hostname/project/blah.git#commit-ish
git+https://user#hostname/project/blah.git#commit-ish
The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.
In fact, it's easier still to use a Github.com repo as a dependency:
As of version 1.1.65, you can refer to GitHub urls as just "foo": "user/foo-project". For example:
{
"name": "foo",
"version": "0.0.0",
"dependencies": {
"express": "visionmedia/express"
}
}
So, to use the Sails GitHub repo, simply use:
"dependencies": {
"sails": "balderdashy/sails-mongo",
...
}
And to use the exact state of Sails as it exists on GitHub as of April 28, 2014, use:
"dependencies": {
"sails": "git://github.com/balderdashy/sails-mongo#b9cdce9a48",
...
}
I had a similar issue. Via the NPM Registry I was trying to get the latest from a project I saw in in GitHub, like this:
//package.json
"devDependencies": {
"foo-package": "^3.3.0",
}
But the code I got back from npm install (as observed in the node_modules/ folder) was not what I saw in GitHub repository's master branch. I was confused; as the two didn't match.
I eventually found: https://docs.npmjs.com/cli/view, which reveals some information (versions and dates) of what the NPM Registry is aware of for a particular repository.
// Console example
npm view foo-package
After confirming that what I wanted from GitHub repository's master branch wasn't in the NPM Registry, I eventually changed my approach Git URLs as Dependencies, just as #apsillers answers.

How to specify local modules as npm package dependencies

I have an application which has the usual set of dependencies on third party modules (e.g. 'express') specified in the package.json file under dependencies. E.g.
"express" : "3.1.1"
I would like to structure my own code modularly and have a set of local (meaning on the file system I am currently in) modules be installed by the package.json. I know that I can install a local module by running:
npm install path/to/mymodule
However, I don't know how to make this happen via the package.json dependencies structure. Using the --save option in this command is simply putting "mymodule": "0.0.0" into my package.json (doesn't reference the filepath location). If i then remove the installed version from node_modules, and try to re-install from the package.json, it fails (because it looks for "mymodule" in the central registry, and doesn't look locally).
I'm sure the is a way of telling the "dependencies": {} structure that I want it to be installed from a file system path, but don't know how.
Anyone else had this problem?
Thanks.
npm install now supports this
npm install --save ../path/to/mymodule
For this to work mymodule must be configured as a module with its own package.json. See Creating NodeJS modules.
As of npm 2.0, local dependencies are supported natively. See danilopopeye's answer to a similar question. I've copied his response here as this question ranks very high in web search results.
This feature was implemented in the version 2.0.0 of npm. For example:
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
Any of the following paths are also valid:
../foo/bar
~/foo/bar
./foo/bar
/foo/bar
syncing updates
Since npm install <folder> adds the package in the directory as a symlink in the current project any changes to the local package are automatically synced.
See: Local dependency in package.json
It looks like the answer is npm link: https://docs.npmjs.com/cli/link
I couldn't find a neat way in the end so I went for create a directory called local_modules and then added this bashscript to the package.json in scripts->preinstall
#!/bin/sh
for i in $(find ./local_modules -type d -maxdepth 1) ; do
packageJson="${i}/package.json"
if [ -f "${packageJson}" ]; then
echo "installing ${i}..."
npm install "${i}"
fi
done
After struggling much with the npm link command (suggested solution for developing local modules without publishing them to a registry or maintaining a separate copy in the node_modules folder), I built a small npm module to help with this issue.
The fix requires two easy steps.
First:
npm install lib-manager --save-dev
Second, add this to your package.json:
{
"name": "yourModuleName",
// ...
"scripts": {
"postinstall": "./node_modules/.bin/local-link"
}
}
More details at https://www.npmjs.com/package/lib-manager. Hope it helps someone.
You can just add to your package.json file in your project
"package-name" : "path/to/package"
and then run npm i in your project
At work we have a common library that is used by a few different projects all in a single repository. Originally we used the published (private) version (npm install --save rp-utils) but that lead to a lot of needless version updates as we developed. The library lives in a sister directory to the applications and we are able to use a relative path instead of a version. Instead of "rp-utils": "^1.3.34" in package.json it now is:
{
"dependencies": { ...
"rp-utils": "../rp-utils",
...
the rp-utils directory contains a publishable npm package
use install-local
I had issues with conflicting react installations from the local dependency.
I solved the error by using install-local npm package. This package does not create symlinks, which solved my issue.
Steps:
run npm i -g install-local
run npx install-local --save <local-path> inside the target repository to install the local dependency
Further reading: https://www.npmjs.com/package/install-local
The error I received, when trying to install the local package with npm install --save <local-directory>:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
You might have mismatching versions of React and the renderer (such as React DOM)
You might be breaking the Rules of Hooks
You might have more than one copy of React in the same app
If it's acceptible to simply publish your modules preinstalled in node_modules alongside your other files, you can do it like this:
// ./node_modules/foo/package.json
{
"name":"foo",
"version":"0.0.1",
"main":"index.js"
}
// ./package.json
...
"dependencies": {
"foo":"0.0.1",
"bar":"*"
}
// ./app.js
var foo = require('foo');
You may also want to store your module on git and tell your parent package.json to install the dependency from git: https://npmjs.org/doc/json.html#Git-URLs-as-Dependencies

Resources