npm link removes child dependencies - node.js

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....

Related

npm install by default tries to install globally

When performing npm install on a folder, the package is installed on c:\Users\<user>\node-modules instead of .\<project folder>\node-modules
I've tried to update npm config save=false but this didn't solve the problem
PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> npm install express
npm WARN danielk No description
npm WARN danielk No repository field.
npm WARN danielk No license field.
+ express#4.17.1
updated 1 package and audited 126 packages in 2.004s
found 0 vulnerabilities
PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> dir
Directory: C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 21/08/2019 7:54 AM 27 app.js
When doing npm install express in the project folder, I would expect the node-modules subfolder to be created in the project folder and the express module to be installed in the node-modules subfolder. However its created in C:\Users\danielk\node-modules.
Could anyone help on what is wrong and how this can be fixed?
When you do npm install, npm actually installs the package to the "npm project" you are currently in. See the following example:
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (stackoverflow)
[...Truncated...]
Is this OK? (yes)
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ mkdir child
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ cd child/
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN stackoverflow#1.0.0 No description
npm WARN stackoverflow#1.0.0 No repository field.
+ express#4.17.1
added 50 packages from 37 contributors and audited 126 packages in 3.021s
found 0 vulnerabilities
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ..
child/ node_modules/ package.json package-lock.json
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ../node_modules/
accepts/ escape-html/ mime/ safer-buffer/
array-flatten/ etag/ mime-db/ send/
body-parser/ express/ mime-types/ serve-static/
bytes/ finalhandler/ ms/ setprototypeof/
content-disposition/ forwarded/ negotiator/ statuses/
content-type/ fresh/ on-finished/ toidentifier/
cookie/ http-errors/ parseurl/ type-is/
cookie-signature/ iconv-lite/ path-to-regexp/ unpipe/
debug/ inherits/ proxy-addr/ utils-merge/
depd/ ipaddr.js/ qs/ vary/
destroy/ media-typer/ range-parser/
ee-first/ merge-descriptors/ raw-body/
encodeurl/ methods/ safe-buffer/
What happens is, I made a stackoverflow folder, initialized an "npm project" there with npm init, and cd into child folder. When I do npm install express inside, the express module will inadvertently get installed to stackoverflow/node_module. This is such that when you are making a program, e.g. myprogram, and even when you are in some subfolder inside (e.g. myprogram/lib/) and you perform npm install, the module will still be installed to myprogram.
Comparing this to your case, this is probably because your C:\Users\danielk\ is already an npm project, so when you are in C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp , npm thinks you are in the C:\Users\danielk\ project and thus save the express module there.
As for why C:\Users\danielk\ became an npm project, is it either you manually did npm init there before, or you performed your first npm install there, and C:\Users\danielk\node_modules is created, marking it an npm project.
And one more thing, that express is not installed "globally", you do "global" install by npm install express -g (well though for express there is no use to install it globally). Your case is just the package getting "installed in the home directory".

Is there a single command to synchronise npm packages in `package.json`?

Let's say, I have install some new packages, uninstall some packages, update some packages to new versions. All changes are saved into package.json. When I pull the changes with this new package.json, is there a single command to do the synchronisation between the locally installed packages and those specified in the updated package.json?
I am looking for something like:
$ npm syncrhonise
npm install will install all packages specified in package.json. npm update will do the same but will also go a find any new versions of those packages. You can uninstall an individual package using npm uninstall <package>. There are already solutions to remove all packages here
I found two ways:
First npm prune will uninstall everything not listed in your package.json
npm prune [<name> [<name ...]]
This command removes "extraneous" packages. If a package name is
provided, then only packages matching one of the supplied names are
removed.
Extraneous packages are packages that are not listed on the
parent package's dependencies list.
Documentation available at prune.
Second You could remove your node_modules/ folder and then reinstall the dependencies from package.json.
rm -rf node_modules/
npm install
This would erase all installed packages in the current folder and only
install the dependencies from package.json. If the dependencies have
been previously installed npm will try to use the cached version,
avoiding downloading the dependency a second time.
Windows Trick
Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:
npm install rimraf -g
rimraf node_modules

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)

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

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