yarn global add . fails
What is the current behavior?
Running yarn global add . in the directory of a local module results in:
[1/4] Resolving packages...
error Received malformed response from registry for undefined. The registry may be down.
What is the expected behavior? This may or may not be a question/feature request, but I'm intending to perform the equivalent to
npm install -g .
whereby the local module would be installed from the local code base to exist as a globally installed module. Is this currently possible?
Please mention your node.js, yarn and operating system version.
Yarn: 0.23.2
Node: 6.10.2
OS: elementary OS Loki
@bestander triage; This sounds like missing NPM compatibility / feature request.
NPM documents this behavior here:
npm install (in package directory, no arguments): Install the dependencies in the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
To add to this, yarn global add file:$PWD is not the same as npm install -g . as it copies the project and reinstalls all dependencies as opposed to just symlinking it.
Just confirmed that this is still an issue. Any help welcome.
I'd been keen to fix this up, if someone could give me some guidance. Just hit it.
If it's not obvious, the workaround is mentioned in @kozhevnikov comment:
yarn global add file:$PWD adds a local package as a global package using yarn v1.10.1
It's also worth noting that npm i -g . actually behaves more like yarn global add link:$PWD
Doesn't work with workspaces:
workspace-root$ yarn global add link:./package1
// -- snip
warning "[email protected]" has no binaries
Done in 2.40s.
workspace-root$
Obviously package1 has binaries, and the version is not 0.0.0
Running in the dir:
workspace-root/package1$ yarn global add link:$PWD
// --- snip
error Couldn't find package "@scope/package2@*" required by "link:/workspace-root/package1" on the "npm" registry.
workspace-root/package1$
Obviously package2 is a dependency of package1 and it's defined in the same workspace.
To clarify, yarn global add expects a package file, not a package folder, as illustrated in the following. My package.json is:
{
"name": "markov-pwgen",
"version": "1.0.0",
"description": "Markov chain password generator",
"dependencies": {
"foswig": "~3.0.1",
"minimist": "^1.2.5"
},
"scripts": {
"preinstall": "test -f dictionary.js || ./scripts/filter-wordlist.py"
},
"type": "module",
"main": "index.js",
"bin": {
"markov-pwgen": "bin/markov-pwgen.js"
},
"repository": {
"type": "git",
"url": "https://github.com/revolution-robotics/roadrunner-debian",
"directory": "contrib/markov-pwgen"
},
"author": "Andrew L. Moore",
"license": "MIT"
}
and running yarn global add on the directory fails whereas npm install -g seems to succeed:
$ yarn global add markov-pwgen:$PWD
yarn global v1.22.10
[1/4] Resolving packages...
error An unexpected error occurred: "markov-pwgen:%2fopt/src/alm/markov-pwgen.js: Invalid URI \"markov-pwgen:%2fopt/src/alm/markov-pwgen.js\"".
info If you think this is a bug, please open a bug report with the information provided in "/home/alm/.config/yarn/global/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/global for documentation about this command.
$ npm install -g
> [email protected] preinstall /home/alm/.nvm/versions/node/v14.15.4/lib/node_modules/markov-pwgen
> test -f dictionary.js || ./scripts/filter-wordlist.py
/home/alm/.nvm/versions/node/v14.15.4/bin/markov-pwgen -> /home/alm/.nvm/versions/node/v14.15.4/lib/node_modules/markov-pwgen/bin/markov-pwgen.js
+ [email protected]
added 3 packages from 3 contributors in 0.369s
But as noted by @partheseas, npm install -g merely creates a link back to $PWD. The solution is to first run yarn pack or npm pack to create a package archive, and then install the archive:
$ yarn pack --no-lockfile .
yarn pack v1.22.10
success Wrote tarball to "/opt/src/alm/markov-pwgen.js/markov-pwgen-v1.0.0.tgz".
Done in 0.11s.
$ yarn global add file://$PWD/markov-pwgen-v1.0.0.tgz
yarn global v1.22.10
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "[email protected]" with binaries:
- markov-pwgen
Done in 3.37s.
The install destination differs between npm and yarn. In my case, yarn installs under $HOME/.config/yarn/global/node_modules whereas, since I'm using the node version manager, nvm, npm installs under $HOME/$NVM_BIN/../lib/node_modules.
yarn global add turbo
The 'yarn global' commands have been removed in 2.x - consider using 'yarn dlx' or a third-party plugin instead
In Yarn 3, the yarn global command has been deprecated. However, you can still run global binaries using yarn dlx.
So, to use turbo for example globally without actually installing it, you can use:
yarn dlx turbo
In Yarn 3, the yarn global command has been deprecated. However, you can still run global binaries using yarn dlx.
So, to use turbo for example globally without actually installing it, you can use:
yarn dlx turbo
useful to know, but the issue in I'm using an older node module that expects certain global binaries to be installed so it can run them from a child process, so that doesn't solve the issue.
You must install additional module [npm install [email protected] --save]!
It does not work properly on some OS! If you will get an error try use other module.
--------------------
node:internal/validators:95
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received type string ('MODULE_NOT_FOUND')