How to prefer global npm rather than local one, inside npm scripts? - npm

I have an almost weird scenario:
My project depends on cordova-lib#6.5.0 package, which has npm#^2.10.x as it's dependencies!
The worst thing about it (I think) is that it takes away my global npm while running npm scripts.
beside its a bad thing, should avoid using it, maybe an update fixes this, etc ... I am looking for a way/hack/workaround for situations like this.
Suppose I have a script in my package.json:
"scripts":{
"libs:update":"npm update bizotop-common-ui-components"
}
when I run npm run libs:update, the local npm (2.10.x) runs instead of my global one (6.1.0).
If I have to keep having npm#2.10.x as my local dependency, do you know any hack/workaround so that I can run my global version of npm in my npm scripts in this kind of situation?
Thanks! +1

Related

Manually updating one line of code in an NPM module does not get carried over to app.js when running npm run dev

Now I know that it's really not good practice to manually adjust anything in an NPM module :) But in this case there's a tiny error on one line in one module that is not being regularly updated. So rather than cloning the Github repo and basically creating my own custom module I really just want to correct that one line.
So I did that, but when I then run npm run dev or npm run prod it's not copying over the changed code. I tried npm install, even npm update, but it's never reflecting the updated code in app.js. Any ideas what I need to do to get it to pull in the updated code?

npm basics clarification: differences between npm start and npm build

Hi I need some clarification on npm materials.
What are the differences between "npm start" and "npm build"?
When do we use "run" for example, what are the differences between "npm test" and "npm run test"?
Thank you so much! I appreciate the explanation.
What you are finding is that there are some default scripts in NPM. Some of these are:
npm start
npm build
npm test
These are simply just aliases for npm run xxxx. To answer your question, npm run test and npm test are exactly the same. npm test is just a shorthand alias.
These default scripts are there to be used as kind of "universal" commands. For example: you have two different projects that have two different build processes. However, you could run npm build in both to build their respective build processes.
It depends on what you're using. In a react app npm start actually does npm run start but npm have allowed a shorthand version.
If you look in your package.json you'll see a scripts parameter that has all the things you can run using npm run [command]. You can define your own ones in there as well.
To answer your first question. The start and build commands are usually defined by webpack.
start is usually used to serve your app locally. So you can go to localhost and see it running.
build is used to compile your app into a folder, usually called dist/, into a flat html/CSS/JavaScript website so you can put the files onto a production server.

NPM Init and Start

I'm using npm for a local project and I want to know if I have to use npm init every time I start a session? I think the answer is yes.
If I restart my machine for example, do I have to do npm init? Do I have to do npm install and npm start each time?
Thanks
No, you only have to do npm init when you're first creating a project. It essentially just creates the package.json file (https://docs.npmjs.com/cli/init.html).
And you should only have to run npm install when you first set up a project for local development, or when changes are made to the project's dependencies. So, usually just once, unless you've made changes. (https://docs.npmjs.com/cli/install.html)
npm start is a script that should be defined in your package.json, and you will likely need to run that every time you begin local development on your project.
When you are creating a node project, you need to have package.json. npm init is a convenient way of scaffolding your package.json; you may need to run it everytime you are starting a new project.
npm install, however, installs your dependencies in node_modules folder. You may need to run this everytime you manually add a dependency to your package.json file.
If you need extra information, check here: https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/
npm init is to make new modules you dont ever need to run npm init to start a session at all as far as I can tell

Save peer dependencies in npm with a command

Is there a way to achieve that using npm ? Currently I do this manually, would be nice to use similar approach as with npm install --save
I found some old discussion and commits but it seems it didn't make it:
https://github.com/npm/npm/pull/3994
As far as I can tell, you can't. Just install it as a regular dependency (production or otherwise, just like the package requiring the peer dependency is installed as).
Even if you manually add the entry to peerDependencies an npm audit is going to fail to recognize the package and tell you to install it.
This kind of stinks, I'm a big fan of the separation of concerns, and keeping a list of modules that only exist so they can be absorbed by other modules is crummy.
But, it is what it is and so long as you leverage the npm commands afforded to you, I guess it's manageable.
Since 'I don't know what npm version' you can use npm i --save-peer package_name command. Works on npm 8.1.0

Webpack uninstall not working properly

I decided to remove webpack from one of my react projects as dependencies slow down performance and I wasn't really using it. I tried npm uninstall -g webpack, npm uninstall webpack, npm uninstall webpack webpack-cli, npm remove webpack, etc. I ran npm start after each of these commands to start working on my app again and every time I got the same exact answer: "Error: cannot find module 'webpack'". It seems to be based in the internal loader for the react-scripts but I don't know how to edit that. Thank you in advance for any help you can provide. Note: I did check my dependencies and webpack is not there in either the main or developer dependencies
Since you said something about react-scripts, i'll assume you are using CRA.
The whole build process is inside the react-scripts package, which means that you would need to NOT use react-scrips on you start and build scripts. If you get rid of react-scripts you might be able to get away with webpack.