npm install fails because package is missing in registry - node.js

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)

Related

npm link removes child dependencies

I am trying to do local development of an NPM package and test it in a package which depends on it. I'm using NPM (7.5.3) and specifically npm link for this but running into a problem with the chain of dependencies.
The child package has dependencies, these are all added to the parent's node_modules folder when using npm install "git+https://github.com/name/child_package". But when I npm link that module:
cd child_package
npm link
cd ../parent_package
npm link child_package
With the last command run (npm link child_package), all of the dependencies for child_package which were in the node_modules of parent_package are removed. NPM reporting:
removed 60 packages, changed 1 package, and audited 231 packages in 1s
At which point all the compilation in the parent package fails due to the missing deps. It finds the child_package, which is symlinked as expected, but dependency defined in child_package of "gsap" has now been removed.
If I reinstall it using npm install "git+https://github.com/name/child_package" it will add the deps back into the node_modules folder of the parent project.
try to do the following:
cd child_package
npm install
that will install child dependencies to directory of child package
personally I hate npm link and always use npm publish (use version number like 1.0.0-preview.1 for your child package and remove '-preview.Number' when you are done)
This is a behavior introduced in npm V7 + .
The only reasonable "workaround" i have found is to go back to npm 6 (npm install -g npm#6).
Another "workaround" is to npm install --no-save ../../my-local-module but to reflect changes to the local module you will need to delete it from node_modules and reinstall again. Kind of lame....

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.

Clarification of the --save option for npm install

First experiences with node.js/npm. From the npm-install docs I read:
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
But I can't understand how it works in practice. If, for example, I run the command:
npm install bower --save-dev
I'd expect to find a package.json file in the current directory with devDependencies set to the installed version of bower, instead I find nothing.
Am I doing/expecting something wrong?
Using node v0.10.21, npm 1.3.12 on Ubuntu 12.04 x64
npm won't create package.json for you, but it will create the necessary dependencies for you as long as package.json exists and is legal JSON.
Create it like so
echo {} > package.json
Then, doing npm i --save whatever will add whatever#~x.x.x as a dependency as expected. The file needs to be there, and be JSON, that's it.
npm install only fetches the packages from the registry and puts them in your ./node_modules. It updates your package.json to register this new dependency if you tell it to.
Your package.json has three dependency blocks :
dependencies - these are needed for your app to run.
devDependencies - these are needed for the developer environments for your app (this is how your teammates can get the packages that you recently added to the project. The dependencies listed here are not required on production hosts.)
optionalDependencies - These packages are optional and it is OK if npm cant reolve the package to install. i.e a build failure does not cause npm install to fail (Note however, that your app should handle cases where the package cannot be found.)
Here is the behavior with the different usages of the npm install command:
$ npm install async #Only installs, no change made to package.json
$ npm install async --save #Installs, adds async#version to dependencies block
$ npm install async --save-dev # Installs, adds async#version to the devDependencies block
$ npm install async --save-optional # Installs, adds async#version to the optionalDependencies block

configure custom module for Node.js project

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.

How generate nodejs express dependencies package.json

As I started to develop my first nodejs express application, I added many packages with npm.
I would like to know if there is a way to generate a package.json file containing all the current dependencies or the list of the current packages under the nodes_modules directory.
Just run npm init in the same directory as your project.
You'll be asked a list of questions (name, version, description, etc.) and once complete, it will generate a package.json file with all of the dependencies as currently installed in the node_modules directory.
Run npm list to see what you have installed. Run npm shrinkwrap to build a npm-shrinkwrap.json file, which you can use as a starting reference to build a proper package.json. My workflow is always to update package.json and then run npm install. I never run npm install foo to get some package because it creates risk of forgetting to add it to package.json and then having your application fail to start on deployment.
Updated to add: These days I do run npm install --save foo or npm install --save-dev foo since I have now decided the ~0.4.3 version numbers it adds to package.json are better than my former preference for 0.4.x since the ~ gives you a more precise minimum version number.

Resources