nodejs / snap

Node.js snap source and updater
MIT License
166 stars 38 forks source link

Corepack not available #26

Closed hterik closed 2 years ago

hterik commented 2 years ago

Corepack is supposed to be included in Node > 16.10, but needs to be activated using corepack enable. When installing node with snap it seems like something is missing for this to work. Steps to reproduce:

$ sudo snap install node --classic --channel=16
node (16/stable) 16.13.0 from OpenJS Foundation (iojs✓) installed
$ node --version
v16.13.0
$ corepack enable
corepack: command not found

I can still see the corepack binary is there under /snap/node/current/bin/corepack. But running it gives some other error:

$ /snap/node/current/bin/corepack enable
Internal Error: not found: corepack
    at getNotFoundError (/snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13867:17)
    at /snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13914:18
    at new Promise (<anonymous>)
    at step (/snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13911:21)
    at /snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13928:22
    at new Promise (<anonymous>)
    at subStep (/snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13926:33)
    at /snap/node/5534/lib/node_modules/corepack/dist/corepack.js:13937:22
    at /snap/node/5534/lib/node_modules/corepack/dist/corepack.js:5369:5
    at /snap/node/5534/lib/node_modules/corepack/dist/corepack.js:5402:5
rvagg commented 2 years ago

OK, so this is probably going to be complicated.

PATH=$PATH:/snap/node/current/bin corepack enable will work because your error there is that it can't find corepack in the PATH, but you'll get another error because it's going to want to set up binaries in the Snap's bin directory, which it's not allowed to touch. You could PATH=$PATH:/snap/node/current/bin corepack enable --install-directory=/usr/local/bin to install it there.

I'm not sure the corepack model is going to be suitable for the Snap because of this need to install in a different path. It's just going to confuse users I suspect.

yarn and yarnpkg are already installed with the Snap, along with npm and npx. So only pnpm and pnpx are missing I think from what corepack offers? I think if these are genuinely worthwhile to you and maybe other users then you could help just get it installed straight into the Snap itself, making corepack unnecessary.

Unfortunately it's not a straightforward task. Have a look at https://github.com/nodejs/snap/blob/HEAD/snapcraft.yaml.sh, which generates https://github.com/nodejs/snap/blob/HEAD/snapcraft.yaml for each release line. If you get yourself setup to develop Snaps locally you can build them straight from these configs and try them out on your local machine. Have a look in the file for yarn and you'll see there's a couple of things that need doing: (1) download and install the package into the right place, and (2) set up aliases for /snap/bin—although we do have to get some official permission from Snapcraft to enable pnpm and pnpx as globals for the package but that shouldn't be a problem I think.

revyh commented 2 years ago

Unfortunately, corepack is not the same as a set of preinstalled package managers. It allows users to specify versions of package managers (via the "packageManager" field in package.json) for many projects. So, for example, I can specify yarn@2.1.0 for package A and yarn@3.2.0 for package B

rvagg commented 2 years ago

Yeah, sorry, I think https://github.com/nodesource/distributions is going to be a better option for this kind of customisability; attempting to make the Snap more flexible than it is and solve for more bespoke situations is an uphill battle that I don't think we can afford to fight at the moment; if it's even possible. Unless someone has a novel contribution to make here for packing, I think this one is off the table for the Snap.

igorpupkinable commented 1 year ago

I have tried @rvagg proposed workaround, but it has its own issues.

First: you need to run it as root to be able to write to /usr/local/bin. Second: check symlink paths. 7222 is the version of a snap. Symlinks will break as soon as you upgrade Node.js snap few times. Ideally it should link to current version of the snap.

sudo -i
PATH=$PATH:/snap/node/current/bin corepack enable --install-directory=/usr/local/bin
ls -Al /usr/local/bin/

lrwxrwxrwx 1 root root 62 Feb 28 11:34 pnpm -> ../../../snap/node/7222/lib/node_modules/corepack/dist/pnpm.js
lrwxrwxrwx 1 root root 62 Feb 28 11:34 pnpx -> ../../../snap/node/7222/lib/node_modules/corepack/dist/pnpx.js
lrwxrwxrwx 1 root root 62 Feb 28 11:34 yarn -> ../../../snap/node/7222/lib/node_modules/corepack/dist/yarn.js
lrwxrwxrwx 1 root root 65 Feb 28 11:34 yarnpkg -> ../../../snap/node/7222/lib/node_modules/corepack/dist/yarnpkg.js

ls -Al /snap/node/
drwxr-xr-x 9 root root 197 Nov  4 21:42 6895
drwxr-xr-x 9 root root 197 Feb 21 22:05 7222
lrwxrwxrwx 1 root root   4 Feb 25 17:44 current -> 7222

I do not see an easy way of getting this working permanently.

r0gi commented 1 year ago

I got around this by installing corepack via npm

npm uninstall -g yarn pnpm  # if you somehow had yarn installed via npm
sudo npm install -g corepack

after which

corepack enable

worked fine.

The only caveat is that yarn is still installed via snap, so in order to use yarn via corepack, you must prefix yarn commands with corepack, e.g.

corepack yarn install

On Ubuntu 20.04, node version v18.15.0, npm version 9.5.0