Run `yarn remove <dependency_name>` to remove dependency, but yarn.lock still shows the removed dependency - node.js

In my node.js project, I had using yarn installed the dependency #nestjs/jwt, now I want to uninstall it since I am not using it.
I run yarn remove #nestjs/jwt. It was successful. I checked my package.json, it was removed. But when I check the yarn.lock file, it is still showing. Why is that?
My git add -p yarn.lock shows me:
-"#nestjs/jwt#8.0.0", "#nestjs/jwt#^8.0.0":
+"#nestjs/jwt#^8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/#nestjs/jwt/-/jwt-8.0.0.tgz#6c811c17634252dd1qcd5dabf409db4692b812da"
integrity sha512-fz2LQgYY2zmuD8S+8UE215anwKyXlnB/1FwJMLVR47clNfMeFMK8WCxmn6xd0hF5JKuV1crO6FVabb1qWzDxqQ==

Besides packages you explicitly install, packages depend on other packages. To see a graph of any dependents of this package you have installed, do:
yarn why #nestjs/jwt -R

Yarn.lock is what yarn uses to know what versions of each dependency are installed so it can get those exact versions again when you run yarn install on a new machine. Try running 'yarn upgrade'. This should create a new yarn.lock file without those dependencies.

Related

Problem with installing react dependencies

I am trying to install react using this command: npx create-react-app .
Then I tried to run using this command: npm run start
Then I get this error:
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"webpack-dev-server": "3.10.3"
Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack-dev-server was detected higher up in the tree:
C:\Users\oa\Desktop\node_modules\webpack-dev-server (version: 3.11.0)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
As suggested here [https://stackoverflow.com/questions/42308879/npm-err-code-elifecycle][1] ,
I have tried to fix it using the following commands:
delete package-lock.json
npm cache clean --force
npm install
npm start
But did not fixed for me.
Just to let you know, my npm version is 6.13.6, and node version is v13.8.0
Try to create a .env file in the root directory of the project and add this line SKIP_PREFLIGHT_CHECK=true inside the file.
then try to run yarn start or npm start.
Did you give a project name after npx create-react-app 'appName' ?
If you have multiple node_modules folders with different versions of dependencies one may conflict with the other - do the following:
Follow steps 1-4 that are initially provided in project directory and commit updated changes locally~
i. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
ii. Delete node_modules in your project folder.
iii. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
iv. Run npm install or yarn, depending on the package manager you use.
Find the location of the node_modules folder that is not in your project folder ~ this line - (C:\Users\oa\Desktop\node_modules\webpack-dev-server)
Go there and delete it
once done try npm start
should work :)!

Cannot start npm/yarn

I decided to create React app under Linux and I've used those commands:
npm install -g create-react-app
create-react-app my_app_name
npm start
At this point I've recieved message:
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"webpack": "4.19.1"
Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack was detected higher up in the tree:
/home/ktw/node_modules/webpack (version: 4.24.0)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /home/ktw/node_modules/webpack is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls webpack in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed webpack.
I've done literally every step from this list but unfortunately - with no results.
I've already tried:
chown -R user: node_modules,
npm cache clean --force,
rm -rf node_modules && rm ./package-lock.json && npm install
and I have no idea what is going on.
Do you guys have any idea how to fix that?
It looks like you may have forgotten to do Step #3: Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
If you are doing the steps as you outline above, when you do npm install again, because Webpack is still in your package.json, it'll reinstall and continue the issue.

Exclude dev dependencies when publishing npm package

So I'm in the process of publishing a package to npm. It is basically just a simple module that lets users make Ajax calls and can be configured in a few ways.
I have read that it is a good idea to test the install locally and tried that. I have packed the package via the "npm pack" command, change into another directory and then tried installing the packge via "npm install path-to-file-that-was-just-created.tgz".
So far everything works, I have a node_modules folder, that contains my bundled code.
However, is also has installed all the dependencies that I have listed as devDependencies in the package.json of my actual module, even though the only the bundled file is needed and no other depenedencies are defined.
I have tried updating the npm-shrinkwrap.json, and checked that every dependency has the dev property marked as true.
The goal is actually for the user to install this module and then have no dependencies installed, because they do not need babel or mocha, to run the module.
How can I exclude these from the packge?
Thanks!
https://docs.npmjs.com/cli/install
use the --production flag to avoid installing dev dependencies
For published modules, you don't need to do anything, when a user installs your library, only the non-dev dependencies will be installed
If you want your published module to have no dependencies but you still need to have some to build it you can also try to use this command before publishing:
npx json -f package.json -I -e "delete this.devDependencies"
This way only works in CI/CD.
Update: it turned out that npm pkg delete devDependencies does the same without any additional dependency
After running your install, you can prune dev dependencies by running this command:
npm prune --production
this will keep only production dependencies. Documentation from npm here:
If the --production flag is specified or the NODE_ENV environment
variable is set to production, this command will remove the packages
specified in your devDependencies

Why do Node modules go into .staging folder?

I have an Electron app that I'm trying to install node modules for. When I run npm install, it creates the node_modules folder but all the modules go into a subfolder called .staging. Each module also has -xxxxx appended to it, where the x's are some random alphanumerics.
Other Electron apps I've created have never done this. All the node modules sit in the root of node_modules and don't have -xxxxx appended.
Any idea why this is happening?
I was also facing the same issue, I tried the steps below:
Delete package-lock.json
Delete Node Modules folder
Try installing it using below command (should be in open network)
npm install
Note: - ".staging" means, those dependencies are getting downloaded so for the temporary basis it keeps all those dependencies under ".staging" folder. Once all gets downloaded properly then it will showcase them under node_modules only.
I hope this will work.
This only happens temporarily until the modules are downloaded and installed. Node seems to do this so it can place together common submodules from all the modules you are installing so it can better structure the node modules folder(mainly for windows users).
If this is happening after an npm install finishes it is likely that there is something wrong with your node installation or something in the install failed.
If you're automatically installing node_modules using CI/CD you should check out npm ci. Also check out this Stackoverflow question.
npm ci
The documentation points out the differences between npm install and npm ci.
The project must have an existing package-lock.json or npm-shrinkwrap.json
If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock.
npm ci can only install entire projects at a time: individual dependencies cannot be added with this command.
If a node_modules is already present, it will be automatically removed before npm ci begins its install. This is nice, because it prevents having to do something like rm -rf node_modules.
It will never write to package.json or any of the package-locks: installs are essentially frozen.
.staging is a temporary npm folder, where the modules are temporarily saved while they are being downloaded, if the package.json downloads are still not completed, the created folder remains, until the installation is complete.
The problem may be lack of space on your hard drive.
I was having 2 versions of node installed on my system.
nodejs v4.2 and node v8.6
I thought this could be conflicting, so I deleted nodejs v4.2 with following commands.
sudo apt-get remove nodejs
and linked the path with
sudo ln -s /usr/bin/node /usr/bin/nodejs
Again I ran npm install and it got fixed
Delete package.lock.json
Delete node_modules
run npm update
This worked for me
I moved the project from C drive to other drive and ran the following commands
take a backup of older node modules if you are running this and existing project
npm cache clean --force
npm update
I faced similar issue and tried the above answers but it did'nt worked for me;
I followed below steps to resolve this issue-
1.npm audit
By running npm audit I got list of pending packages to install-
2.npm i packagename
After installing one or two package one by one from list, I used
3.npm install
At this time the installation went smooth without any lag or hangup. Hope this help who is facing similar issue :).
Sometimes the cache is corrupt and also unremovable.
This fixed the issue I was experiencing.
If you are using nvm
Get the current node version node --version
nvm uninstall (that version)
nvm install (that version)
nvm use
npm install
If you have a windows machine where you do not posses Admin rights to it.
Try deleting node_modules and install using 'npm install' from command line as
'ADMINISTRATOR'
It works!
Anyways, it comes down to an open network thing ;)

How to sync `yarn.lock` with `package.json`?

I installed a package with yarn add --dev, run its setup process and during it, the package installed several other packages and added those to package.json (in devDependencies), I assume with npm. Great, but now my yarn.lock is out of sync.
What is the correct, non-manual way of syncing yarn.lock to the current state of package.json?
Edit: yarn check shows the missing packages as:
error Lockfile does not contain pattern: <package>#<version>
But it doesn't add them.
Run yarn install, or just yarn.
The lock file is updated in its entirety on any change to dependencies, i.e. when you run a yarn command.
From the Yarn docs:
Your yarn.lock file is auto-generated and should be handled entirely by Yarn. As you add/upgrade/remove dependencies with the Yarn CLI, it will automatically update your yarn.lock file. Do not edit this file directly as it is easy to break something.
(Emphasis my own)
If you ever face a checksum issue this will solve it,
YARN_CHECKSUM_BEHAVIOR=update yarn

Resources