'npm install --only=dev' deletes existing packages - node.js

While working on a node project, I am having trouble separating installation of application and test dependencies using npm. I am using node version 8.1.2 and npm version 5.0.3.
To elaborate, I am using docker to create production and test images for my node application with the idea that the production image will have only the application dependencies installed (e.g. aws-sdk, xml-builder). I am doing this by running npm install -q --only=prod in the production docker image.
The test image extends the production image and installs the test dependencies (e.g. chai, mocha) on top of it. This is achieved by running npm install -q --only=dev command in the test docker image. The purpose of this exercise is to create a clean production image that doesn't have unnecessary packages.
But when I execute the latter npm command (npm install -q --only=dev) it removes the packages installed by the first install. I'm doing the same thing in another project that uses an older version of npm and node and it works fine.
Did something change in the latest version of npm? If so is there another prescribed way of achieving the same effect?

It's not because of the new version of Node.js, but because of the new version of npm that is bundled together (version 5).
In your case you could do one of these:
Execute first npm install --only=prod in your production image, and then just npm install in your test image.
Deleting package-lock.json after the first npm install.
Using the option --no-package-lock in each npm install.
The new npm version uses a new file called package-lock.js, the one producing this behaviour, more info here.
EDIT:
I just found out this is an issue with npm, it seems it will be fixed in the next release. At the moment the workarounds I wrote above should work.

Related

Why npx create-react-app is showing "the project was bootstrapped with an old unsupported version of tools"?

I recently switched to Ubuntu, whenever I create react app it's giving me this note, and the app it's creating is a class component.
ankit#gram:~/Documents/Development/React/react-project$ npx create-react-app my-app
Creating a new React app in /home/ankit/Documents/Development/React/react-project/my-app.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...
added 987 packages in 39s
22 packages are looking for funding
run `npm fund` for details
Success! Created my-app at /home/ankit/Documents/Development/React/react-project/my-app
Inside that directory, you can run several commands:
npm start
Starts the development server.
npm run build
Bundles the app into static files for production.
npm test
Starts the test runner.
npm run eject
Removes this tool and copies build dependencies, configuration files
and scripts into the app directory. If you do this, you can’t go back!
We suggest that you begin by typing:
cd my-app
npm start
Happy hacking!
Note: the project was bootstrapped with an old unsupported version of tools.
Please update to Node >=14 and npm >=6 to get supported tools in new projects.
my node version: v16.14.0
my npm version:8.3.1
I think this was because npx was not installed in my system, when I created a react app by this command create-react-app the app created was fine. I also installed npx using this command npm install -g npx and created a react app and everything was fine. before this make sure you have installed create-react-app in your system if not then install it by using this command
sudo npm install -g create-react-app
I am a new programmer had this issue not long ago I just solved it.
Use sudo apt install nodejs
It will automatically install version 10 for you which doesn't accept React.
Then you try to install NVM package, use it to update your node to a newer version.
You can check me on GitHub bari racha Wuye
I was having the same problem after I had just installed Ubuntu 22.04.
The fix for me was updating node and npm by following these instructions free code camp update node and npm
Specifically, I followed the instructions under the heading "1. Use NPM to Update Your Node Version", to update node and ignored the 2nd and 3rd headings, then followed the instructions under the last heading to update npm
afterwards I restarted my computer, opened the project directory and ran the command
npm i create-react-app#latest
then I ran
npx create-react-app projectname
and it worked as expected

node wont update angular

I have been struggling for days to try and get angular to update. I have finally narrowed it down to the fact when I run
ng new
it creates an angular app using version 1.7.4 but I have updated angular to version 8.1.2. when I run
ng --version
I get the message "your global version is 8.1.2 is greater than your local version 1.7.4 the local version will be uses"
I have run npm
npm update -g#angular/cli
npm update #angular/cli
but nothing. I have also deleted the files in
c:\users\user\appdata\roaming\npm\node_modules\#angular
and tried running
npm install #angular/cli
Has anyone got any idea either how to update my local install of angular or when I run
ng new
to get it to use the global version?
Thanks
npm update will update your package to the latest safe version, i.e. it won't increase a major version of your current package which is 1 in your case. A change of a major version means there will be breaking changes.
So you need to explicitly specify the package version to which you want to migrate. I.e. npm update #angular/cli#^8.1.2 or npm install #angular/cli#^8.1.2, sorry I mostly use yarn so not sure which of these 2 commands will work.

Fresh install of npm and node

I have recently started working with Angular 2 and am unable to get the Angular 2 Quickstart project to run correctly due to a number of errors in my npm dependencies.
Am I able to globally uninstall everything that was previously installed with npm to allow me to do a clean install of it and any required dependencies?
Note: The errors are the same as these examples which are caused by packages needing to be installed globally, however, the errors still occur having followed these steps...
To check your global installed packages you can type:
npm ls -g --depth=0
That lists all global installed packages with depth=0. That mean that it doesn't output dependencies of the packages. You can uninstall global packages with:
npm uninstall -g package-name
Please do not uninstall the npm package itself... But you can update your npm version with npm:
npm install npm -g
As mentioned in the Article your Node.js version should be at least v4.x.x and the npm version should be v3.x.x. You can get the installed versions with these commands:
node -v
npm -v
Updating your Node.js depends on your Operating System. Assuming that you use Windows you should uninstall the current version via control panel and download an actual release from the official Node.js page. https://nodejs.org/en/download/current/
To get a great overview how npm works you should consider reading their
documentation: https://docs.npmjs.com/
Make sure you have the correct node.js version. The guide says 5.0 or greater but points you to the wrong download link.
Try this: https://nodejs.org/en/download/current/
With this node you should be able to follow the guide step by step.

NPM package has outdated dependency, is there a way to alter its version?

Just migrated to Node 4.1.2 from 0.10. One of the packages being installed via npm install errors due to node-gyp having a problem with one of its dependencies, it's quite a few versions out of date. The issue has been brought up on the repo but hasn't seen activity since May. Is there a way to tell NPM to install this package but with the outdated dependency using a newer version?
EDIT:
I've copied over an installed version from node_modules in an older project. npm install -g npm-check-updates then ncu in the node_modules/bs-html-injector/ directory. It lists updates, ncu -u will update the package.json, npm install after. I run my gulp task and html is injecting fine, all seems fine :) Would still like to know how to do this if I didn't have a local copy installed by NPM. It looks like it's just a 1:1 copy from the github repo?
With npm, you can install packages from GitHub directly:
npm install user/repo#branch
You can fork the package on GitHub, make and propose the changes you need and use your fork as a dependency in your project until PR is merged.
you can use --force to force install it

running npm install unbuilds global npm

I have a project that depends on nodegit.
When I run
npm install -g .
I get this output at some point:
When npm finishes installing, I lose access to it in the path:
I end up having to run 'n', to change to a previous version of node, then run 'n' again to change to the latest version of node and in the end run 'npm install -g npm#latest' to get the latest version of npm.
It looks like you are trying to install NodeGit. (Or maybe you are installing something that depends on NodeGit?) Assuming you are installing NodeGit, that is not a CLI tool and should not be installed globally. It installs npm as a dependency. Your installation is failing for whatever reason, and as part of cleanup, it is uninstalling npm.
If you are installing NodeGit, do not install it with -g.
If you are trying to install something that isn't NodeGit, you need to either change directories to that project folder or (more likely) change your installation command line.
If you are installing something that depends on NodeGit and really should be installed globally, you'll need to identify why your installation is failing (which it appears to be failing at the pruning of dev dependencies, but I can't tell why from the output--might be file permissions, might be something else). As long as NodeGit installation fails, it will unbuild npm.
This is actually an issue with the nodegit project.
You can follow it in https://github.com/nodegit/nodegit/issues/561 which led to https://github.com/nodegit/nodegit/issues/567
Nothing i can do but wait.

Resources