Can I install packages from the node_modules directory? - node.js

So a friend has sent me his node_modules directory but I don't want to manually type npm i *package* for each package and I don't have the package.json file to npm install from.
Is there a possible way to install the packages in the node_modules directory via terminal?
It is a very backwards scenario, I'm just curious if there's a way...

You can running:
npm ls -json > tree.json
node -e "console.log(Object.entries(require('./tree.json').dependencies).map(([m, v])=>m+'#'+v.version))"
It will print
[ 'fastify#2.13.0', 'fastify-url-data#2.4.0', 'pino-pretty#3.6.1' ]
for my test

Related

NPM installing to ~/.npm directory

I have to run npm install --prefix ./ --save bootstrap jquery for it to save in the node_modules. I expect from what I've read in tutorials and such this is not the expected behavior. When I just run npm install --save bootstrap it puts the files into /home/philip/.npm directory as /home/philip/.npm/bootstrap.
npm root gives the correct [project]/node_modules directory.
NPM Version: 3.10.10
Node Version: 6.10.3
OS: Ubuntu 17.04
Edit: Forgot to ask the question, how do I ensure npm install defaults to the project's node_modules directory?
It might be because global flag is set to true somewhere in npm config or via an environment variable.
To check current value run: npm config ls -l | grep global. To change it try to add global=false to your ~/.npmrc file. Also, check the value of $NPM_CONFIG_GLOBAL, it has higher priority than .npmrc.

npm install in a specific local folder

I'm currently trying to execute the command npm install to get all dependencies and modules necessary to run my package.json.
The problem is that I don't have Internet access to fetch from the internet, so I have downloaded the node_modules on a different PC and copy-paste it on my local folder that contains all. If I tried to run npm install without arguments, it's still trying to fetch from internet and fails.
I have read their documentation and apparently they have listed few npm install that takes different arguments, but still, I'm unable to install from the folder already downloaded.
I've tried to do npm install node_modules on the path that contains the package.json, but nothing. I'm running on windows 7.
If someone has an approach to specifying the local node_modules and just install all modules inside, I'll appreciate.
Thanks!
You probably should use npm-link...
From the docs:
Go to the node_modules directory, and, inside each package, run npm-link:
$ cd node_modules
$ cd package-name
$ npm link
$ cd ..
...
In the directory of your project which needs the local modules:
$ npm link package-name

npm install resulting in 'ENOENT: no such file or directory'

I've installed Node.js for Windows and I'm trying to install a package via npm. The command prompt is in the directory of the project (C:\Users\username\Desktop\NodeTest), which contains a single helloworld.js file. Upon typing 'npm install express', I receive the following error:
ENOENT: no such file or direcotry, open 'C:\Users\username\package.json
I'm attempting this from a clean install and cmd is running as admin.
Any ideas?
I was facing the same issue. I firstly delete my node_modules and delete the cache by following command:
rm -rf node_modules && npm cache clean --force
then I delete the package-lock.json file from my project and then hit npm install in command prompt and it works.
As already pointed out by Subburaj this is because you are missing a package.json.
Just run npm init to initialize that file for you; afterwards it should work.
If you are working on a Windows machine using Vagrant/VM, there's a chance that symlinks are the culprit for your problem. To determine if that is the case, simply copy your package.json and package-lock.json into a test directory that is not mounted/shared between OSs.
mkdir /tmp/symlinktest
cd {{your directory with your package*.json}}
cp package*.json /tmp/symlinktest
cd /tmp/symlinktest
npm install
If this results in a successful install, you'll need to either exclude the node_modules directory from the mount (there's various articles on doing this, however I can't say I've had success) or run npm install outside the mounted volume.
I deleted the package-lock.json and It worked for me.
Basically I was Offline while I tried to install with npm, so go online and try
npm install again
Check the project folder which you opened in microsoft visual code. Generally you are not in the right path so npm is not able to search the package.json ... My project was in Document/hostel/hostel .. I opened Document/hostel ... So npm tried to find the package.json in Documents folder .. When i entered one level inside to Document/hostel/hostel .. it was fixed.

How to clean node_modules folder of packages that are not in package.json?

Assume I install project packages with npm install that looks into package.json for modules to be installed. After a while I see that I don't need some specific module and remove its dependency from package.json. Then I remove some other modules from package.json because they are not needed anymore and others are replaced with alternatives.
Now I want to clean node_modules folder so that only modules listed in package.json stay there and the rest must go, something like npm clean. I know I can remove them manually but would like to have some nice ready to use sugar functionality for that.
I think you're looking for npm prune
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.
See the docs: https://docs.npmjs.com/cli/prune
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.
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
From version 6.5.0 npm supports the command clean-install (ci) to hard refresh all the packages.
Please, see the references:
npm 6.x: npm-ci | npm Docs.
npm 7.x: npm-ci | npm Docs.
npm Blog Archive: Introducing npm ci for faster, more reliable builds.
simple just run
rm -r node_modules
in fact, you can delete any folder with this.
like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.
just open the gitbash move to root of the folder and run this command
Hope this will help.
First globally install rimraf
npm install rimraf -g
go to the path using cmd where your node_modules folder and apply below command
rimraf node_modules
Just in-case somebody needs it, here's something I've done recently to resolve this:
npm ci - If you want to clean everything and install all packages from scratch:
-It does a clean install: if the node_modules folder exists, npm deletes it and installs a fresh one.
-It checks for consistency: if package-lock.json doesn’t exist or if it doesn’t match the contents of package.json, npm stops with an error.
https://docs.npmjs.com/cli/v6/commands/npm-ci
npm-dedupe - If you want to clean-up the current node_modules directory without deleting and re-installing all the packages
Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.
https://docs.npmjs.com/cli/v6/commands/npm-dedupe
Have you tried npm prune?
it should uninstall everything not listed in your package file
https://npmjs.org/doc/cli/npm-prune.html
The best article I found about it is this one: https://trilon.io/blog/how-to-delete-all-nodemodules-recursively
All from the console and easy to execute from any folder point.
But as a summary of the article, this command to find the size for each node_module folder found in different projects.
find . -name "node_modules" -type d -prune -print | xargs du -chs
And to actually remove them:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
The article contains also instructions for windows shell.
I have added few lines inside package.json:
"scripts": {
...
"clean": "rmdir /s /q node_modules",
"reinstall": "npm run clean && npm install",
"rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod",
...
}
If you want to clean only you can use this rimraf node_modules or rm -rf node_modules.
It works fine
You can also use npx in combination with rimraf to remove all node modules with one command, so you don't have to install rimraf first.
So go to the path where your node_modules folder is located by using cmd (in case you are not there already) and run the following command
npx rimraf node_modules
For Windows User, alternative solution to remove such folder listed here: http://ask.osify.com/qa/567
Among them, a free tool: Long Path Fixer is good to try: http://corz.org/windows/software/accessories/Long-Path-Fixer-for-Windows.php
rimraf is an package for simulate linux command [rm -rf] in windows. which is useful for cross platform support. for install its CLI:
npm install rimraf -g
If you are using YARN in your project please add below line to your pacjage.json script and run yarn clean to execute on project terminal
"clean": "rm -rf yarn.lock node_modules ios/Podfile.lock && yarn install && cd ios && pod install && pod update && cd .."
Use following command instead of npm install
npm ci

How to npm install to a specified directory?

Is it possible to specify a target directory when running npm install <package>?
You can use the --prefix option:
mkdir -p ./install/here/node_modules
npm install --prefix ./install/here <package>
The package(s) will then be installed in ./install/here/node_modules. The mkdir is needed since npm might otherwise choose an already existing node_modules directory higher up in the hierarchy. (See npm documentation on folders.)
As of npm version 3.8.6, you can use
npm install --prefix ./install/here <package>
to install in the specified directory. NPM automatically creates node_modules folder even when a node_modules directory already exists in the higher up hierarchy.
You can also have a package.json in the current directory and then install it in the specified directory using --prefix option:
npm install --prefix ./install/here
As of npm 6.0.0, you can use
npm install --prefix ./install/here ./
to install the package.json in current directory to "./install/here" directory. There is one thing that I have noticed on Mac that it creates a symlink to parent folder inside the node_modules directory. But, it still works.
NOTE: NPM honours the path that you've specified through the --prefix option. It resolves as per npm documentation on folders, only when npm install is used without the --prefix option.
In the documentation it's stated:
Use the prefix option together with the global option:
The prefix config defaults to the location where node is installed. On
most systems, this is /usr/local. On windows, this is the exact
location of the node.exe binary. On Unix systems, it's one level up,
since node is typically installed at {prefix}/bin/node rather than
{prefix}/node.exe.
When the global flag is set, npm installs things into this prefix.
When it is not set, it uses the root of the current package, or the
current working directory if not in a package already.
(Emphasis by them)
So in your root directory you could install with
npm install --prefix <path/to/prefix_folder> -g
and it will install the node_modules folder into the folder
<path/to/prefix_folder>/lib/node_modules
There currently is no documented way to install a package in an arbitrary directory. You should change into the target directory, make sure it has a package.json, and then use the regular commands.
While there currently is an undocumented option called --prefix, this feature might be removed in a future release. At least, it it is not planned to ever document this as an official feature.
I am using a powershell build and couldn't get npm to run without changing the current directory.
Ended up using the start command and just specifying the working directory:
start "npm" -ArgumentList "install --warn" -wo $buildFolder

Resources