Is there a way to restore missing package.json? - npm

I'm working on react projects, then i want to upload it to github pages, so i follow some tutorials, and there are steps to install gh-pages via npm. So i install it, but i terminate the process by pressing ctrl+c, and it stop the process. And i feel i have to uninstall it, so i do npm uninstall gh-pages --save-dev. The process isn't finished, and my laptop overheat and died (old laptop sorry).
And i went back, i want to check the packages. After running npm list --depth=0, its showing so much extraneous error, no such file directory and and path ended with package.json. My package.json on each modules missing, i've been checked it, only left 1 package.json on root folder.
and much more error messages
Is there a way to get it back all?

Run commands sequentially:
rm -rf node_modules package-lock.json
npm i
Then if you want to install that package again, run
npm i gh-pages

Related

Is there a way to install an npm package locally but not affect package.json or package-lock.json?

I have a project that I'm working on for a client where I have two private packages (which I can't get access to npm install) are inside the package.json.
I do however have access to clone the repos for those said packages. If I simply run an npm install I'll get a permission denied error. Same if I run npm link to the packages.
I've been working around this by removing the packages from the package.json then running npm install ../some-package. This works but isn't a great solution because if I wanted to add a new package I'd have to deal with a bit of a mess with the package.json.
Is there a better way than this?
I have tried running npm link ../some-package but I still get access denied. The only way I've managed to complete an install is by removing the packages then installing them from a local dir.
I don't know the details of your situation, but I see at least two potential solutions to explore.
Option 1: Install the package from the repo
I do however have access to clone the repos for those said packages.
You can install from a git repo and package.json will record that git repo as the source of the package rather than the npm registry.
From the docs at https://docs.npmjs.com/cli/v8/commands/npm-install:
npm install :
Installs the package from the hosted git provider, cloning it with git. For a full git remote url, only that URL will be attempted.
Option 2: Install from the local file system with --no-save
If that approach doesn't work for you, you can try npm install --no-save ../some-package as a build step. The --no-save makes it so it doesn't modify package.json.

HRM module stop working on the same project on which it is working earlier

Hot reload module stop working on my project because of which i have to run this command each time i make any changes.
npm run serve
This usually is npm packages problem and is often remedied by reinstallation
first remove the node_modules directory in project root
also you can clear the npm cache npm cache clean --force(this is optional)
then reinstall packages npm i

Cannot Find Module

I am trying to run react-native start and the following error appears
"Cannot find module 'metro-core'. Run CLI with --verbose flag for more details.
Prior to getting this message, I had a different error message saying modules was not located, so I tried this:
Delete the node_modules folder -
rm -rf node_modules && npm install
Reset packager cache - rm -fr $TMPDIR/react-* or node_modules/react-native/packager/packager.sh --reset-cache
Clear watchman watches - watchman watch-del-all
I just typed in the react-native start and the error message popped up on the simulator, which told me to look at my terminal for the error message.
This is common with NPM. Do not worry. Just follow a few steps and you will get your package.
Step 1: $ npm cache clean --force
Step 2: delete node_modules by $ rm -rf node_modules folder or delete it manually by going into the directory and right-click > delete.
Step 3: npm install
To start again, $ npm start
This worked for me. Hopes it works for you too.
Still, if it is there, kindly checks the error it displays in red and acts accordingly.
Be careful when using rm -rf.
After that,
While working on Unix systems.
Sometimes it may not allow you to install such packages. For that, you need sudo permissions.
Sometimes, the package is installed but only in your local modules, and when you try to import(require) it from outside of the directory, the error occurs.
Sometimes, your compiler read your dependencies, but not able to find this package in that, at that time also you face this error.
Anyways, don't worry. You just have to follow some steps below.
A best practice is to initialize your project using npm init before starting development. This will initialize your project and generate package.json file. (Ignore it if your project have package.json file)
Then, if you want any library as dependencies, try --save with npm install command. This will save your dependency in package.json file.
e.g. npm install metro-core --save
If any package is not found after installing, install it globally by -g flag.
Globally installed packages will be accessible within your system. e.g. npm install metro-core -g.
Note: Unix system needs SUDO permission for installing it globally.
I hope this will help you.
npm install metro-core
use command then run

How to set npm not to install packages that had been installed globally?

My project references mocha, phantomjs, etc, which takes a lot of time to download during npm install. This is not a problem in my local machine because I only download them once and can use them forever unless I decide to manually upgrade them.
However, in my CI machine, my jenkins server need to download them every time that I did a git commit and git push to do the testing and deploy.
So can I just speed up that process by set the npm not to download these slow packages from the remote server? Rather, install them from local cache or not to install them if I installed them globally?
Anyone knows how to configure that?
I found some packages that might be helpful
npm-install-changed will run npm install only if the contents of package.json's devDependencies and dependencies were changed, note that it assumes that node_modules persists across different builds which might not be helpful if your CI server always start from scratch
npm-install-cache runs npm install and then copies your current node_modules folder (to somewhere in \tmp), if you call the script again it will verify any changes to package.json (instead of changes done on devDependencies or dependencies), if it didn't change then it will copy the node_modules folder stored in \tmp, the only limitation I see is that it's not cross platform and that the cache folder is \tmp which is erased on reboot (or maybe even when a is process finished!)
The second package might not work as it is but it seems like a good place to start :)
You can specify all of the packages you want to use locally in devDependencies in package.json, and then running npm install -d will install those instead of the main dependencies.

Does npm delete downloaded module if the installation fails?

When I try to install something with npm it fails quite often (much more often that apt-get for example), and it will display "see log file for details" or "make in the directory failed". But when I try to inspect the directory said it will not be found. Does NPM simply delete every thing it just downloaded if anything fails during installation? Why would it tell me to check the directory then if it deleted it?
npm keeps downloaded packages as tarballs inside a cache folder.
see: https://www.npmjs.org/doc/cli/npm-cache.html
When you run npm install and something goes wrong, it will try to undo and remove the packages from your current location, but it should leave the cached tarballs alone. Sometimes the cache can have a bad package-tarball.
You can force npm to install without using the cache like this npm install --force. Or, if you really must, you can clear out the whole cache like this npm cache clean.
Remember: npm installs packages into the current folder, or wherever your package.json can be found