'npm-view command fails when running with yarn

I'm struggling to understand why running the same sript using npm, yarn and node gives different results: npm view <private-package>:

node (v16.13.2) - Working!

> node
Welcome to Node.js v16.13.2.
Type ".help" for more information.
> require('child_process').execSync('npm view @coti-cvi/cvi-sdk').toString('utf8')
'\n' +
  '\x1B[4m\x1B[1m\x1B[32m@coti-cvi/cvi-sdk\x1B[39m@\x1B[32m0.2.26\x1B[39m\x1B[22m\x1B[24m | \x1B[32mISC\x1B[39m | deps: \x1B[36m12\x1B[39m | versions: \x1B[33m7\x1B[39m\n' +
  'CVI SDK\n' +
  '\x1B[36mhttps://github.com/cotitech-io/cvi-sdk#readme\x1B[39m\n' +
  '\n' +
  'keywords: \x1B[33mcvi\x1B[39m\n' +

npm (8.1.2) - Working!

> npm run node-from-package-json-scripts

> [email protected] node-from-package-json-scripts
> node

Welcome to Node.js v16.13.2.
Type ".help" for more information.
> require('child_process').execSync('npm view @coti-cvi/cvi-sdk').toString('utf8')
'\n' +
  '\x1B[4m\x1B[1m\x1B[32m@coti-cvi/cvi-sdk\x1B[39m@\x1B[32m0.2.26\x1B[39m\x1B[22m\x1B[24m | \x1B[32mISC\x1B[39m | deps: \x1B[36m12\x1B[39m | versions: \x1B[33m7\x1B[39m\n' +
  'CVI SDK\n' +

Also:

> npm view @coti-cvi/cvi-sdk

@coti-cvi/[email protected] | ISC | deps: 12 | versions: 7
CVI SDK

yarn (1.22.17) - not working - 404 error!

> yarn node-from-package-json-scripts
yarn run v1.22.17
$ node
Welcome to Node.js v16.13.2.
Type ".help" for more information.
> require('child_process').execSync('npm view @coti-cvi/cvi-sdk').toString('utf8')
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.yarnpkg.com/@coti-cvi%2fcvi-sdk - Not found
npm ERR! 404

Package.json:

{
  "name": "p1",
  "scripts": {
    "node-from-package-json-scripts": "node"
   }
}

I think that yarn inject some enviroment variables that confuse npm-view command.



Solution 1:[1]

I found a solution, I'm not sure why this solution works, but it does.

require('child_process').execSync('npm view @coti-cvi/cvi-sdk', {cwd: '/Users/stavalfi/projects/cvi-swissknife', env:{npm_config_registry:'https://registry.npmjs.org'}}).toString('utf8')

Basically, add npm_config_registry:'https://registry.npmjs.org' as enviroment variable to override what yarn injects:

npm_config_registry="https://registry.yarnpkg.com"

which causes the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stav Alfi