Understanding difference between npx and npm - npm

I'm struggling to develop a deeper understanding of npx. So in particular the difference between running a commmand with npm and npx. I understand that npx can execute a package from a URL, just one local npm package etc.. But for example here:
npx lerna run start --scope frontend --stream
What is the difference between
npx lerna run start
and
npm lerna run start
?

NPM - Manages packages but doesn't make life easy executing any.
NPX - A tool for executing Node packages.
NPX comes bundled with NPM version 5.2+.
NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.
When executables are installed via NPM packages, NPM links to them:
1.local installs have "links" created at ./node_modules/.bin/ directory.
2.global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

Related

npm start install my package, but i'm running my project with yarn start

If I installed some package with NPM it will work and recognize it if I run my project with yarn start instead npm start?
Yes, it will still work after all the required packages are installed.
But you should not switch package Managers like that since you will generate two lock files if you use both to install packages. npm generates the package-lock.json and yarn will generate the yarn.lock file. I recommend you to stay with one or switch completely.
In runtime, it does not make any difference if you use yarn or npm.
npm run start and yarn start will do exactly the same.

Cannot find module 'postcss-preset-env' error using create-react-app inside an npm workspace

I'm trying to add a create-react-app app inside a Turborepo monorepo.
Problem is that if I run npm install from the root of the monorepo and then try to start the CRA app by running npm run dev, I get the following error during compile time:
Loading PostCSS "postcss-preset-env" plugin failed: Cannot find module 'postcss-preset-env'
I noticed that if I run npm install from within the CRA app folder (/apps/my-app), and then run npm run dev from the root of the monorepo, it runs just fine.
I also noted that depending from where I run npm install, the contents of /apps/my-app/node_modules will be different, but I think is the expected behaviour.
If I understand correctly the npm workspaces docs, you should always run npm install from the root of the monorepo.
Any idea on what I'm missing here?
I'm using npm 8.3.1.
Here is a public repo with an example: https://github.com/oncet/turborepo-cra

What is the NPM equivalent of "yarn install --frozen-lockfile"?

I'm using npm as part of me building the production docker image.
I want to make sure the package-lock.json doesn't change and matches.
You can use npm ci.
npm ci bypasses a package’s package.json to install modules from a package’s lockfile. This ensures reproducible builds—you are getting exactly what you expect on every install.
https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable

npm 5 install folder without using symlink

Before publishing my node library, I could use the advice the npm documentation wrote about:
To test a local install, go into some other folder, and then do:
cd ../some-other-folder
npm install ../my-package
Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.
However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:
npm install :
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?
Thank you.
Use npm pack + npm install (as suggested by install-local package)
npm pack <path-to-local-package>
npm install <package-version.tgz>
This will effectively copy your local package to node_modules.
Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:
my-package
package.json
test
test-app
package.json
node_modules
my-package
Assuming that test dir is not included in the files in my-package/package.json.
This works the same way with npm 5 and older versions.
I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink.
https://www.npmjs.com/package/npm-install-offline
npx npm-install-offline ../some-package
Or
npx npm-install-offline my-npm-package --repo ./my-offline-npm
It also will install the package dependencies which npm does not do with local packages.

When installing locally (npm install .), can I have npm use my global packages?

When I run npm install . it takes a while to build packages that contain c code like expresso (which depends on node-jscoverage). I realized that I can copy expresso from my global package directory (~/Developer/lib/node_modules/expresso) to ./node_modules/expresso in my current directory before running npm install . and it won't bother compiling it. Is there a way to tell npm to try to install packages from my global npm directory before fetching and building them?
I guess this command might help: npm link
Check out this: npm to install packages from local position rather than from web?