how to automate dependency resolution (local and remote) in node - node.js

I have a very small data-cap so I want to install dependencies for node projects by first linking to whatever it finds installed on local machine and fetching remotely if not found locally. I don't want to manually go through all the require statements in the apps because theres a lot of them - is there a node way to do this?
eg. this except not manually determining what is or isn't installed
Project A
npm install -g connect
npm install -g serve-static
then later
Project B
npm link connect
npm link serve-static

The preferred way to go about dependency management is with a package.json.
To get started with one of these in a project, run npm init, and answer all the questions. This will leave you with a file like this:
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC"
}
Once this is in place, add the --save flag to each of your npm install commands. For example, if you run npm install --save connect, your package.json changes to this
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC",
"dependencies": {
"connect": "^3.3.5"
}
}
Now whenever you need to install the project's dependencies, run npm install, and all the dependencies listed in the package.json will be automatically installed.

Related

can not setup ReactJS on mac

I need to setup ReactJS on my Mac. I have npm version of 4.1.2 and node version of v7.7.4. I clone the project from Git and in the project folder trying to execute npm install and following npm start. But I get error.
I removed&uninstalled node and npm and then re-installed with homebrew, but it did not help. Here is the screen of error I get in terminal? Do you have any idea what is the problem?
Here is my package.json file
{
"name": "viaopt",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"webpack": "^2.3.0"
},
"description": ""
}
From the error you are showing I can tell that there is no start script in your package.json file. For npm start to work there should be a command under the start key under the scripts attribute in your package.json file.
When you run npm start npm looks in package.json and runs whatever is listed there under the start value.
I'm guessing you have gulp or webpack configured with your project?
Try running webpack in your terminal and see if your project fires up or smth.
Hopes this helps.
Cheers.
If your trying to setup a new React App you can use
Create React App. It will setup React / Webpack for you.

package.json -- How to add already installed packages in 'dependencies' and 'devDependencies' in package.json file after delete package.json?

I have added many packages in my Project using npm.
But after while cleaning my Project i was deleted my package.json by mistake.
so for create new package.json file i have run command nmp init.
now i got the package.json but in my package.json file is like below.
{
"name": "name-will-be-here",
"version": "0.0.1",
"description": "description-will-be-here",
"main": "gulpfile.js",
" dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is there any commands for fill the details like before it is...?
but my Question is that...
How can i get all details of dependencies and devDependencies which was already there before delete?
Short answer is you cant.
Longer answer is that you need to understand how npm 3 works by default and that is to install all dependencies and shared sub dependencies in same top folder so your node_modules holds a lot more than your main dependencies only (unless you used legacy option or older npm).
In future use scm like git or ide history features to revert accidental deletions.

Refusing to install 'module' as a dependency of itself

Recently, I was playing with a gulp tutorial and had this error
Refusing to install gulp as a dependency of itself
when executing
npm install --save-dev gulp
what could be the issue?
The problem was in the name of my own application.
In package.json, I accidently named it gulp
{
"name": "gulp",
"version": "1.0.0",
"description": "tutorial",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "rmv",
"license": "ISC",
"devDependencies": {}
}
Make sure your app is not called as one of the dependencies you will be using.
This error occurs when you are trying to install the package from the path (or in the folder) in which you had created the package.. Just navigate to some other folder or path and then try to install the package It should work.

Is it possible to clone the git repositories when installing NPM packages from git?

I have a package.json like this:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"some-module": "git+ssh://git#github.com/IonicaBizau/some-module.git"
},
"author": "",
"license": "ISC"
}
When I do npm install everything is installed correctly, but I would like to install the package from my repository by cloning the repository too, so in node_modules/some-module I will have a git repository cloned from GitHub.
How can I do that? Is there a built-in option for that or should I build my own tools?
I created gpm - a tool that install the dependencies from git.
git + npm === gpm
To install gpm do:
npm i -g gpm
Then you can do:
gpm -i some-module
This will install the dependencies from git repositories, recursively.

npm WARN install Refusing to install hapi as a dependency of itself

I tried to do the following (per instructions from official site):
mkdir hapi && cd hapi
npm init
npm install hapi --save
But this gives me an error like this:
npm WARN install Refusing to install hapi as a dependency of itself
Now, I made a new test folder called hapiTest and repeated the commands and then everything worked fine.
I tried the same process with a folder gulp and npm install gulp --save, and got the same error, so my conclusion is that I can't have the name of the folder be the same as the package that I want to install, but can someone back this statement up with some official documentation?
When you did the command npm init, there were probably some relevant questions you needed to answer. Specifically, the name of your module. When you use npm init, it assumes you want the name of the module you're creating to be called the name of the folder it is in.
So it's not the name of the folder that is stopping you from installing the dependency, it is the name of the npm module that you are creating.
Open the resulting package.json within your hapi directory, and rename the module to something other than hapi. Here's an example 'package.json' that works, even when residing in a folder called hapi:
{
"name": "hapi-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hapi": "^7.5.2"
}
}
Added Note
I've not been able to find any documentation thus-far explaining this phenomena in the context of npm; though it is a bit of a no-brainer. Requiring modules with the same name within the same application would conflict with the CommonJS philosophy.
The name of your module is same as the module you are trying to install. NPM thinks that you are installing the module to itself. Change the name of your module and it will install perfectly.
Reason
Module name is same with library name
Solution
Change the module name to something else
Change 'name' in package.json
The issue can be simply explained as follows
the name of your package or module in package.json cannot be same as that of the package or module you are trying to install.
Here hapi is the name of your module and you are trying to install a module with name hapi with npm install hapi --save
This was my initial code
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
which threw error
npm WARN package.json react#1.0.0 No description
npm WARN package.json react#1.0.0 No repository field.
npm WARN package.json react#1.0.0 No README data
npm WARN install Refusing to install react as a dependency of itself
then i renamed the name from react to react_app and my code looks like
{
"name": "react_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
then it worked

Resources