corepack icon indicating copy to clipboard operation
corepack copied to clipboard

Windows Powershell Disabled by default

Open randomaxess opened this issue 3 years ago • 7 comments

Info Windows: 10 v19045.2251 Node: v18.12.1 Yarn: v3.3.0

Issue Windows does not allow powershell scripts to be run by default.

In full:

yarn : File C:\Program Files\nodejs\yarn.ps1 cannot be loaded because running scripts is disabled on this system. For more inf 
ormation, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ yarn -v
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Replication As administrator run the following console commands: corepack enable corepack prepare yarn@stable --activate yarn -v

Sollution If you have access to overwrite the global powershell policies, this is easy to solve. In many Enterprice settings this isn't going to be an option, however, even when you have the local administrative rights to run the "corepack" commands on your local machine.

What you can do, is to rename the "yarn.ps1" file to something like "yarnABC.ps1". Then the system seems to default to the "yarn.CMD" file instead, which works just fine.

Powershell files in corepack I'm not sure why "corepack enable" creates .ps1 files for the corepack utilities, when there aren't any for npx or npm, but this will cause an issue in a lot of Enterprise settings, and if the .ps1 files aren't necessary, then maybe they could be removed in a future update?

randomaxess avatar Nov 18 '22 10:11 randomaxess

What you can do, is to rename the "yarn.ps1" file to something like "yarnABC.ps1". Then the system seems to default to the "yarn.CMD" file instead, which works just fine.

Another "workaround" is to always prefix the yarn command with "corepack", e.g. corepack yarn start. Not ideal of course, but it might unblock you as it should work in all cases corepack itself is available.

We could introduce a new env variable to disable the creation of .ps1, or maybe a CLI flag (e.g. corepack enable --no-powershell). I'm not familiar enough with Windows to know if not having .ps1 by default would make sense, but making it opt-out would be a good first step I think. Would you like to contribute a PR?

aduh95 avatar Feb 24 '23 18:02 aduh95

This is a definitely a problem for Windows users after upgrading to yarn 4.

asvishnyakov avatar Feb 09 '24 11:02 asvishnyakov

Another option is to add aliases to your PowerShell profile as documented here: https://github.com/nodejs/corepack/tree/b9eea589b4535d32b8e9ca1e884c3a44bf7d99ea#corepack-enable--name

merceyz avatar Feb 09 '24 12:02 merceyz

It's a require additional steps by developers, while previously it was just clone and go solution with yarnPath

asvishnyakov avatar Feb 09 '24 13:02 asvishnyakov

This is off-topic but you can still use yarnPath if you want https://yarnpkg.com/cli/set/version#options.

merceyz avatar Feb 09 '24 13:02 merceyz

I think the real solution for this issue will be just sign this scripts with the same key as used for Node.js itself.

asvishnyakov avatar Feb 09 '24 13:02 asvishnyakov