jjangga0214 / hasura-cli

Hasura CLI as an npm package
https://www.npmjs.com/package/hasura-cli
90 stars 27 forks source link

Error when installing hasura-cli #116

Open tictaqqn opened 5 months ago

tictaqqn commented 5 months ago

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 commented 5 months ago

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

zwily commented 5 months ago

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

tictaqqn commented 5 months ago

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

sandeep17sf commented 5 months ago

same error

oren-nonamesecurity commented 5 months ago

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

image

oren-nonamesecurity commented 5 months ago

Probably related to this failed action - https://github.com/jjangga0214/hasura-cli/actions/runs/8413611293/job/23036098801#step:6:14

bkniffler commented 5 months ago

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

Jeck-Sparrow-5 commented 5 months ago

same here, someone have a solution?

adrianwix commented 4 months ago

@jjangga0214 Some help here?

Destreyf commented 4 months ago

same here, someone have a solution?

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

Nexitisyo commented 4 months ago

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 commented 4 months ago

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 commented 3 months ago

Is this issue still not resolved?

Destreyf commented 3 months ago

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

lkuich commented 2 months ago

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 commented 1 day ago

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

Destreyf commented 1 day ago

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.