Environment variable expansion not longer works in npm run on Windows - windows

I have npm scripts that use environment variables. Here is an excerpt of package.json:
"scripts": {
"staging": "npm run deploy -env=staging",
"deploy": "winrs /r:intranet /d:F:\\wwwintranet\\deploy npm run build -env=%npm_config_env%",
Until now, -env=%npm_config_env% was expanded to -env=staging or whatever. Suddenly it stopped working. Probably this is due to an update to npm from 6.x to 7.x.
When I test with "deploy": "set & winrs /r:intranet /d:F:\\wwwintranet\\deploy npm run build -env=%npm_config_env%", I can confirm that the variable is still set.
As a temporary solution, downgrading npm to version 6.x (npm install -g npm#6) helped. Nevertheless, I need a solution that will work also in the future.
How can I enable variable expansion again for npm 7.x ? Or what are the alternatives for passing environment variables with npm 7.x ?
Edit
This was a bug in npm 7.5.x. It is fixed in npm 7.7.0 and above (see https://github.com/npm/cli/issues/2731)

Failure to expand environment variables on Windows appears to be a recent high-priority known bug in the npm CLI.
There are several options to use as a workarounds until a fix is released.
You can downgrade to version 6.x until this is fixed.
This won't work for your specific case (see below), but for most cases, you can use npx to use npm#6:
npx -p npm#6 npm run deploy
You can try downgrading to an earlier version of 7.x to see if the bug has always existed in 7.x or not. Start with 7.0.0 and go from there.
The npx suggestion won't work here because deploy invokes npm itself and that will use npm from your PATH which is npm#7. But for generic cases where you need to invoke an old version of npm in narrow cases, that will work.

Related

Laravel 9 [webpack-cli] TypeError: compiler.plugin is not a function

I downloaded a laravel 9 template and I am trying to run and use it but every time I try npm run dev I get the error [webpack-cli] TypeError: compiler.plugin is not a function.
I've solved the issue by reinstalling browser-sync-webpack-plugin which, according to the error, is the plugin that causes the error. Here are the following commands that solved my problem:
npm uninstall browser-sync-webpack-plugin
npm install browser-sync-webpack-plugin
It upgraded this plugin's package.JSON's line from "^2.0.1" to "^2.3.0".
I think I should update all my NPM plugins (see NPm commands npm outdated and npm update).
Usually it means that one of the plugins needs to be updated

We no longer support global installation of Create React App

can anyone explain what is this as far as I know I had installed sanity and nodemon globally they are also not allowed ?
PS C:\Users\Rupesh Gupta\Documents\GitHub\PinShare\pinshare_frontend> npx create-react-app#next --scripts-version=#next --template=cra-template#next ./
You are running create-react-app 5.0.0-next.60+6a3315b7, which is behind the latest release (5.0.0).
We no longer support global installation of Create React App.
Please remove any global installs with one of the following commands:
npm uninstall -g create-react-app
yarn global remove create-react-app
The latest instructions for creating a new app can be found here:
https://create-react-app.dev/docs/getting-started/
PS C:\Users\Rupesh Gupta\Documents\GitHub\PinShare\pinshare_frontend>
I had the same issue on that project. I solved it by running
npx clear-npx-cache
and then
npx create-react-app#latest ./ --use-npm
"./" if you're already in the folder you wanted to CRA into, or "your-project-name" if you're not.

yarn berry run how to run installed packages

I see with yarn berry I get the plug'n'play feature instead of node_modules/
I couldn't find anything to suggest it supports running from installed packages.
For example with npm a workflow might be to run the installed version of webpack:
$ npm install --save-dev webpack
$ node node_modules/webpack/bin/webpack ...
A globally installed webpack might not be the same version. Worse yet, during Docker deployment, I get what's installed locally, the only node and npm are available globally. I thought I can do a preinstall script that does npm install -g yarn; yarn set version berry but then I'm not sure how to do webpack, jest, babel, etc, and the thought that I should have to install them all globally during the same preinstall hackaround seems like several steps backwards.
Is there some way to run from locally-installed packages that I'm missing?
I saw this possibly related question - Yarn Berry - Run a Node Script Directly
But the answer there seems a bit off the point - I'm not running any js, I'm trying to type in a package.json script, i.e. something that can run from the shell.
Why not just use yarn run <bin> (or simply yarn <bin>)? If you are in a repository set to use yarn berry, that will run any package bin file.
yarn node <file> will run any .js file with Plug n' Play set up. No need to install those dependencies globally, except for maybe yarn classic.
I was trying to do yarn some-bin and kept getting:
Couldn't find a script named "some-bin".
I eventually figured out it was because the package that provides some-bin is installed inside a workspace and not at the root of my project. So instead I had to run:
yarn workspace my-workspace some-bin
And that worked.

How to install all the dependency package in yarn?

First, I'm new to React. I'm trying to use Google's Material-UI for my React project. In this tutorial, it says run npm install, but I heard using yarn and npm together in the same project because it might bring about some confusion between those two later. So, I'm trying to stick to yarn only.
npm install seems to install all the dependency package for the thing that I wanna use, but how can I do that in yarn? I tried yarn add, but it didn't work. How can I do that?
EDIT
Just found that it has only package.json, which means I can only use npm install to install dependencies. Would there be no problem when I use yarn later?
You should just be able to run yarn ("Running yarn with no command will run yarn install, passing through any provided flags." So just a simple yarn is what you'd want now, this answer previously suggested yarn install)
Here is a comparison table of most/all the commands you'd likely encounter
Edit Sept 2020: The newer versions of npm have greatly improved and caught up to yarn, so I currently have no clue what possible benefits yarn offers anymore, I'm 100% npm for the last year or so
The equivalent of $ npm install is just $ yarn (without arguments) to install all dependencies from package.json.
Also, just to clarify your query -
Just found that it has only package.json, which means I can only use npm install to install dependencies. Would there be no problem when I use yarn later?
Doesn't matter whether you use yarn or npm, there will always be a package.json.
And no, there won't be a problem when using yarn later.
You get package-lock.json when using npm, and yarn.lock file when using yarn.
It's not recommended to use both yarn and npm for the same project, so you should remove either of package-lock.json and yarn.lock.
In my case it worked as - yarn global add #angular/cli (similarly any dependency)
then added path "C:\Users\USER_NAME\AppData\Local\Yarn\Data\global\node_modules.bin" in "Edit the system environment variables" -> Environment Variables -> Under System Variables select PATH-> click New -> Add the above path then save-> Open new command prompt -> run the dependency command.
yarn install --force
From Yarn's help text on the install command:
--force   install and build packages even if they were built before, overwrite lockfile
I had a situation where nom install would install everything and yarn install wouldn't. So maybe try the other package manager?

npm doesn't work with node v0.4.12 mac

I don't know if I had npm and node installed on my mac before running the executable from the nodejs.org website, but at this time I am getting this error Error: npm doesn't work with node v0.4.12 after running the install package.
I can't uninstall npm, the npm command is completely useless.
Please assist if you have insight
I would say the safest thing is to manage Node & NPM versions with NVM (Node Version Manager). It installs (compiles) both Node & NPM for you, for each version you choose.
What's happening is that you have the new version of npm installed(v1.x.x) and an old version of node, e.g. v0.4.12, which are incompatible.
The best thing to do is update node, otherwise you could download the old npm install bash script from http://npmjs.org/install.sh and execute it (you might need to do it as sudo) The script will clean up all other versions of npm and install a version compatible with your node v0.4.12

Resources