Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when installing hasura-cli #116

Open
tictaqqn opened this issue Mar 25, 2024 · 18 comments · May be fixed by #119
Open

Error when installing hasura-cli #116

tictaqqn opened this issue Mar 25, 2024 · 18 comments · May be fixed by #119

Comments

@tictaqqn
Copy link

tictaqqn commented Mar 25, 2024

When running npm install -g hasura-cli, the errors below are shown and hasura-cli won't be installed.
I doubt it is caused by the release at 2:10UTC March 25 (version: 2.28.0).

npm ERR! code 1
npm ERR! path /opt/hostedtoolcache/node/20.11.1/x64/lib/node_modules/hasura-cli
npm ERR! command failed
npm ERR! command sh -c node dist/index.js
npm ERR! node:internal/modules/cjs/loader:1147
npm ERR!   throw err;
npm ERR!   ^
npm ERR! 
npm ERR! Error: Cannot find module '/opt/hostedtoolcache/node/20.11.1/x64/lib/node_modules/hasura-cli/dist/index.js'
npm ERR!     at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
npm ERR!     at Module._load (node:internal/modules/cjs/loader:985:27)
npm ERR!     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
npm ERR!     at node:internal/main/run_main_module:28:49 {
npm ERR!   code: 'MODULE_NOT_FOUND',
npm ERR!   requireStack: []
npm ERR! }
npm ERR! 
npm ERR! Node.js v20.11.1

OS: Linux (GitHub Actions) / Mac
Node version: 20.11.1

@Destreyf
Copy link

I ran into this issue under node 18 as well, my solution was to roll back to the last version (2.36.1).

@zwily
Copy link

zwily commented Mar 25, 2024

Should we be concerned that 2.38 was pushed to npm, but not github?

@tictaqqn
Copy link
Author

I'm not sure how 2.38 was published. Does anyone know about it?

@sandeep17sf
Copy link

same error

@oren-nonamesecurity
Copy link

Same here, too many new versions yesterday, seems odd.

image

@oren-nonamesecurity
Copy link

@bkniffler
Copy link

Same same, the dist folder is missing in the NPM package

@Jeck-Sparrow-5
Copy link

same here, someone have a solution?

@adrianwix
Copy link

@jjangga0214 Some help here?

@Destreyf
Copy link

same here, someone have a solution?

The only solution is to roll back to an older version.

@Nexitisyo
Copy link

Nexitisyo commented Apr 30, 2024

Same here, too many new versions yesterday, seems odd.

That is probably caused by updating directly via the CLI (hasura update-cli).
My current approach is installing an older version, like @Destreyf already said, and then updating it via CLI

e.g.,

npm i -g hasura-cli@2.36.1
hasura update-cli

@andoks
Copy link

andoks commented Apr 30, 2024

Same here, too many new versions yesterday, seems odd.

That is probably caused by updating directly via the CLI (hasura update-cli). My current approach is installing an older version, like @Destreyf already said, and then updating it via CLI

e.g.,

npm i -g hasura-cli@2.36.1 hasura update-cli

I find the whole point of the package to be able to pin the exact version of the cli tool to the same as I use for the hasura server.

While using the update-cli subcommand may be a workaround to get a newer version of the CLI going, it mostlye defeats the point of the npm package.

@zohaibadnan137
Copy link

Is this issue still not resolved?

@Destreyf
Copy link

Unfortunately it's not resolved, I accidentially updated this package and it broke my cli again.

@lkuich
Copy link

lkuich commented Jul 9, 2024

I'm still having this issue as of July, almost 3 months since this issue was opened. This seems like a straightforward solution, are we still maintaining this package?

@zwily
Copy link

zwily commented Sep 5, 2024

I guess this project is dead. Anyone want to fork it and take on doing new releases?

@Destreyf
Copy link

Destreyf commented Sep 6, 2024

I have moved away from using this package but figured I could share what I am doing now.

My setup relies on a docker-compose existing to scan for a specific version, or it'll download the latest version if it can determine the version number from the hasura/graphql-engine github releases page.

All of my hasura commands call a bash script that's located in a tools directory

I've included my download script, and my bash proxy script below.

./tools/download-hasura-cli.mjs

import { argv } from 'node:process';

import {
  createWriteStream,
  chmodSync,
  existsSync,
  readFileSync,
  mkdirSync,
  unlinkSync,
} from 'node:fs';

import { execSync } from 'node:child_process';

import { finished } from 'node:stream/promises';
import { Readable } from 'node:stream';

let tag = argv && argv.length > 2 ? argv[2] : null;

const explicitTag = !!tag;

if (!tag) {
  if (existsSync('./docker-compose.yml')) {
    const dockerCompose = readFileSync('./docker-compose.yml');
    const matches = dockerCompose
      .toString()
      .match(/image: hasura\/graphql-engine:v\d+\.\d+\.\d+$/im);

    if (matches && matches.length > 0) {
      tag = String(matches[0].split(':').pop());
    }
  }
}

if (!tag) {
  // Go find latest
  const res = await fetch(
    'https://github.com/hasura/graphql-engine/releases/latest',
    { redirect: 'manual' },
  );

  if (res && res.headers && res.headers.get('location')) {
    tag = res.headers.get('location').split('/').pop();
  }
}

if (!tag) {
  console.error('Unable to determine hasura tag');
  process.exit(1);
}

tag = String(tag).trim();

const platform = process.platform === 'win32' ? 'windows' : process.platform;
const ext = platform === 'windows' ? '.exe' : '';

const output = `./node_modules/.bin/hasura${ext}`;

if (existsSync(output)) {
  // Check version
  let v = execSync(`${output} version --skip-update-check`).toString();
  try {
    v = JSON.parse(vout).version;
  } catch (e) {
    // Do nothing
  }

  if (v.includes(tag)) {
    if (explicitTag) console.log(`hasura-cli@${tag} is already installed`);
    process.exit(0);
  }

  // Remove old version
  console.log(`Removing current version of hasura-cli@${v}`);
  unlinkSync(output);
}

const parts = [
  'cli',
  'hasura',
  platform,
  process.arch === 'x64' ? 'amd64' : process.arch + ext,
].join('-');

const url = `https://github.com/hasura/graphql-engine/releases/download/${tag}/${parts}`;

console.log(`Downloading hasura-cli@${tag} from ${url}`);

const file = await fetch(url);

if (file.ok) {
  mkdirSync(`./node_modules/.bin/`, { recursive: true });
  const stream = createWriteStream(output);

  Readable.fromWeb(file.body).pipe(stream);

  await finished(stream);

  if (platform !== 'win32') {
    chmodSync(output, 0o777);
  }
}

This has zero dependencies outside of nodejs itself, and uses the native nodejs fetch to download the cli binary directly.

I am on nodejs v20, this script uses root level async/await so you'll need to be on a relatively recent version of node.

If you manually run the download script node ./tools/download-hasura-cli.mjs you can pass in an argument to download a specific version.

node ./tools/download-hasura-cli.mjs v2.41.0

This would download the version for 2.41.0.

./tools/hasura

 #!/usr/bin/env bash

if [ ! -f ./node_modules/.bin/hasura ]; then
  node ./tools/download-hasura-cli.mjs
fi

./node_modules/.bin/hasura "$@"

This bit of bash scripting checks for the hasura cli being installed into the node_modules directory, if it doesn't exist it calls a download script, once we have a cli downloaded it simply calls the CLI with all arguments that were passed to it.

Make sure it's executable and then you can simply use ./tools/hasura in place of npx hasura-cli

To run a migration you can run it like this:

./tools/hasura migrate apply --all-databases

Then additionally I added a postinstall script to my package.json

package.json

{
  "name": "project-name",
  "scripts": {
    "start": "start script",
    "postinstall": "node ./tools/download-hasura-cli.mjs",
    ...
  }
}

With these 2 scripts and the postinstall, my environment works fairly smooth and has the benefit of not having yet another dependency in the package.json to maintain.

The ./tools/download-hasura-cli.mjs script should work under windows, but the ./tools/hasura bash proxy won't, I do not work on windows directly.

@kenjihikmatullah kenjihikmatullah linked a pull request Dec 24, 2024 that will close this issue
@kenjihikmatullah
Copy link

Hi guys, I am opening this PR #119 to solve the issue. Feel free to have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.