Install multiple Node.js Modules based on dependencies (list) - node.js

I have a dependencies list of nearly 40 node modules and while I was able to install all of them using one (or two) commands-
npm install module-1 module-2 ... module-N --save-dev
-still looking for a way to create a file that installs all dependencies when executed. This way anyone could download a single file for his/her system (Mac / Linux in my case) and install all modules by simply running that file. Any great suggestion, method? Thanks a lot!

npm init will create a package.json for you. Within that file you can list your app's dependencies and dev dependencies like this:
{
...
"dependencies": {
"express": "^4.9.7"
},
"devDependencies": {
"mocha": "^2.0.1"
}
}
Then npm install

Related

Why Npm is installing different 40+ modules other other computer but few packages on one

I really dont know why the NPM has started downloading about 40+ modules on Npm install but my package.json only contains following dependencies.
"devDependencies": {
"typescript": "^1.6.2",
"vscode": "0.10.x"
},
"dependencies": {
"fs": "^0.0.2"
}
Following are the list it started. Besides these modules there are about double of this number downloaded and added below but i didnt show here.
Is there any way to reset. I have tried to remove the modules folder and install again it started adding again.
Yesterday when i run i only get these dependencies only on other PC.
HeadCode is correct. npm3 installs some dependencies in a flat way.
From the docs:
While npm2 installs all dependencies in a nested way, npm3 tries to mitigate the deep trees and redundancy that such nesting causes. npm3 attempts this by installing some secondary dependencies (dependencies of dependencies) in a flat way, in the same directory as the primary dependency that requires it.
So, if you are using npm v2.x on one machine, and npm v3.x on another, you can get very different folder structures under your node_modules folder.

"npm install" installs all dependencies in node_modules directory, instead of having them nested

I need to know if the following behavior is normal.
When I npm install, each package from my package.json and the dependencies, don't get installed nested anymore, but each dependency is installed in the node_modules directory. That makes my node_modules directory blown and look like this:
This happened since I updated npm and node.
Now I run:
npm -v 3.3.6
node -v 4.2.1
python 2.7
windows 7
wamp
My package.json file looks like this:
{
"private": true,
"devDependencies": {
"gulp": "^3.8.8"
},
"dependencies": {
"laravel-elixir": "^3.0.0",
"bootstrap-sass": "^3.0.0"
}
}
It's the standard laravel package.json file.
Is there a way to have nested directories again, because I don't like such a blown article with over 100 sub directories.
Update: As Erik Pukinskis mentioned in the comments:
As of npm 3.5, support for --legacy-bundling has been dropped.
Yes, there is a way to have nested directories again by changing npm's (version 3 as of this writing) default behaviour:
Delete the currently present node_modules folder.
Tell npm to install with legacy bundling for this one install:
npm install --legacy-bundling
A "permanent" alternative:
Set your npm config to always use legacy bundling...
npm set legacy-bundling=true
.. and run as usual:
npm install
Note: fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.
Disclaimer: As a non-Windows user I have no need for flat dependencies and want to find self-declared dependencies with ease in favour of automatic deduping. Since installing npm dependencies without legacy bundling already takes an incredible amount of time I'm usually willing to spend those extra minutes install time. It gets back down to 5 directories from previously 700+ (...) in a Laravel Elixir setup with bootstrap (non-sass), font-awesome and jquery added.
That's the new behavior of npm 3 as per this npm blog.

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.

How to install customer node.js binary module globally?

So I've made my own module with C++ and node-gyp. Things go fine after node-gyp configure build and I've got under build/Release everything I need.
Now, in my other project where I'm using this module, I don't want to add it like
var a = require('../../mylib/build/Release/mylib');
but instead
var a = require('mylib');
after defining dependencies in package.json. So how do I use npm or something else to achieve this?
You can add the entry point to your package to the main field in your package.json. This is what will be called when you do require('yourpackage').
See these links:
at end of section 8.1 http://book.mixu.net/node/ch8.html
under section main in https://npmjs.org/doc/json.html
sudo npm install --global /home/nhaa123/mylib/build/Release/mylib (if that is the folder containing your package.json) documentation: https://npmjs.org/doc/install.html
You don't want to install the module globally – project dependencies are meant to be installed locally (in the project's folder). The only thing you install globally are npm modules meant to be run from the command line.
Instead, just add the path to the folder containing your module (assuming it has its own package.json) to your project's package.json.
{
"name": "My Project",
"dependencies": {
"express": "3.1.x",
"mylib": "/home/me/mylib"
}
}

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