johnlindquist / kit

Script Kit. Automate Anything.
https://scriptkit.com
MIT License
3.9k stars 138 forks source link

I updated node/npm, and now Kit is unable to run any scripts #1104

Open SimplGy opened 1 year ago

SimplGy commented 1 year ago

I was in a state where script kit could run the intro script, but nothing with an NPM dependency. So, I updated that stuff.

Now, it can't even run the intro script. So, I wonder if changing the default version of node/npm breaks things. I'm sorry I don't, but I have no idea how to get it into a correct state again.

Background

The commands I ran to update the node environment were something like:

nvm install --latest-npm   
nvm install --lts
nvm alias default lts/hydrogen

I'm looking for a "re-run setup procedure" option maybe?

Current State Error Output

Run this very simple intro script: image

Get this error: image

Cannot find package '/Users/eric/.kenv/node_modules/@johnlindquist/kit/' imported from /Users/eric/.kenv/scripts/browse-scriptkit.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '/Users/eric/.kenv/node_modules/@johnlindquist/kit/' imported from /Users/eric/.kenv/scripts/browse-scriptkit.js
    at new NodeError (node:internal/errors:387:5)
    at legacyMainResolve (node:internal/modules/esm/resolve:341:9)
    at packageResolve (node:internal/modules/esm/resolve:944:14)
    at moduleResolve (node:internal/modules/esm/resolve:1006:20)
    at defaultResolve (node:internal/modules/esm/resolve:1220:11)
    at nextResolve (node:internal/modules/esm/loader:165:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:844:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36)
SimplGy commented 1 year ago

Noting that in finder, /Users/eric/.kenv/node_modules/@johnlindquist/kit/ appears to be an empty folder

johnlindquist commented 1 year ago

@SimplGy the ~/.kenv/package.json should show "@johnlindquist/kit" pointing to ~/.kit

It should be using node from ~/.knode/bin/node regardless of what version of node you have installed elsewhere.

If you want to "reset" the Kit.app:

  1. Quit
  2. Delete ~/.kit and ~/.knode
  3. Open Kit.app and it should go through the install process again
le250055 commented 1 year ago

Getting a similar issue [2023-02-07 16:25:08.618] [warn] ☠️ ERROR PROMPT SHOULD SHOW ☠️ [2023-02-07 16:25:08.619] [warn] Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@johnlindquist/kit' imported from /private/var/folders/14/sk4mzw5s1h9gz1dc2xmgbvc80000gq/T/kit/myfile.mjs at new NodeError (node:internal/errors:387:5) at packageResolve (node:internal/modules/esm/resolve:957:9) at moduleResolve (node:internal/modules/esm/resolve:1006:20) at defaultResolve (node:internal/modules/esm/resolve:1220:11) at nextResolve (node:internal/modules/esm/loader:165:28) at ESMLoader.resolve (node:internal/modules/esm/loader:844:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18) at ModuleWrap. (node:internal/modules/esm/module_job:76:40) at link (node:internal/modules/esm/module_job:75:36)

johnlindquist commented 1 year ago

@le250055 Did anything happen before this error started occurring that you think may have caused it?

SimplGy commented 1 year ago

TL;DR: Confirmed both statements in my environment and "reset" the app, but still see the same error


the ~/.kenv/package.json should show "@johnlindquist/kit" pointing to ~/.kit

Confirmed, looks right:

➜  .kenv  cat package.json 
{
  "type": "module",
  "devDependencies": {
    "@johnlindquist/kit": "file:../.kit",
    "image-downloader": "^4.3.0",
    "sharp": "^0.31.3"
  }
}

~/.kit is not an empty folder, so not sure why I'm seeing Cannot find package '/Users/eric/.kenv/node_modules/@johnlindquist/kit/

It should be using node from ~/.knode/bin/node regardless of what version of node you have installed elsewhere.

Ah, good to see how this works. Confirmed.

-rwxr-xr-x  1 eric  staff    79M Sep 22 20:39 node
lrwxr-xr-x  1 eric  staff    38B Feb  4 18:09 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 eric  staff    38B Feb  4 18:09 npx -> ../lib/node_modules/npm/bin/npx-cli.js
➜  bin  ./node -v
v16.17.1
➜  bin  ./npm -v
8.15.0
➜  bin  npm -v
9.3.1
➜  bin  node -v
v18.14.0

Trying the reset procedure...

➜  ~  rm -rf ~/.kit 
➜  ~  rm -rf ~/.knode 
  1. Restarted the app (noted that it does re-run the setup procedure)
  2. Still see the error noted in the original issue, even for the two line browse(url) script
johnlindquist commented 1 year ago

@SimplGy If you manually link ~/.kit to ~/.kenv like so:

cd ~/.kenv && PATH=~/.knode/bin:$PATH npm i ~/.kit

Does that:

  1. Succeed?
  2. Change what the ~/.kenv/package.json looks like?
SimplGy commented 1 year ago

npm install shows a progress bar for a few moments, then appears to fail in this way:

➜  $  cd ~/.kenv
➜  $  PATH=~/.knode/bin:$PATH npm i ~/.kit
npm ERR! code ENOTDIR
npm ERR! syscall rename
npm ERR! path /Users/eric/.kenv/node_modules/@johnlindquist/kit
npm ERR! dest /Users/eric/.kenv/node_modules/@johnlindquist/.kit-XKBdfT1i
npm ERR! errno -20
npm ERR! ENOTDIR: not a directory, rename '/Users/eric/.kenv/node_modules/@johnlindquist/kit' -> '/Users/eric/.kenv/node_modules/@johnlindquist/.kit-XKBdfT1i'

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/eric/.npm/_logs/2023-02-11T20_14_15_190Z-debug-0.log

...looking...

My PATH is:

➜  $  echo $PATH                                           
/Users/eric/.nvm/versions/node/v18.14.0/bin:/Users/eric/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/opt/subversion/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/eric/bin

That thing is a sym link:

➜  $  lsa /Users/eric/.kenv/node_modules/@johnlindquist/
lrwxr-xr-x    1 eric  staff    13B Feb  4 19:13 .kit-XKBdfT1i -> ../../../.kit

This succeeds (inside .kenv folder)

➜  .kenv  rm -rf node_modules 
➜  .kenv  npm i
# ...
added 472 packages, and audited 473 packages in 16s

after doing that, now this also succeeds:

.kenv  npm i ~/.kit
up to date, audited 473 packages in 1s
johnlindquist commented 1 year ago

@SimplGy Now I'm really curious 😅

(fyi, that npm i command works on my machine 🙃)

SimplGy commented 1 year ago

Back in a working state! 🎉

I think it was rm -rf ~/.kenv/node_modules that fixed it, although I also deleted package-lock.json at some point too.

Wish I could tell you what the problem was, these environment issues must be a real challenge. Really appreciate the help and look forward to building some amazing automations with this tool.

johnlindquist commented 1 year ago

@SimplGy Hrmmmmmmmmmmm 🤔

By any chance, did you ever use yarn inside of ~/.kenv?

SimplGy commented 1 year ago

No, not on purpose/manually.

I haven't used yarn in a long time and I'm sure of that because I can't remember what it's for. 😁

johnlindquist commented 1 year ago

@SimplGy I really appreciate you taking the time to look into it!!! 🙏

Glad we at least have a fix for now.

The setup process literally does npm i ~/.kit just like we did above.

I'm curious if installing sharp or image-downloader caused some strange npm side effect. I can't think of what else may have impacted it...

vhmartinezm commented 1 year ago

Hey @johnlindquist I have a similar issue. I updated to node 18.12.1. I installed canvas and d3-node and I have a weird behavior. My others scripts run in both launcher and cli. But the one which uses canvas works only in the cli. If I run it from the app I got:

[08:32:03.105] [warn] ☠️ ERROR PROMPT SHOULD SHOW ☠️ [08:32:02.970] [info] Found d3-node in /Users/victormartinezmejias/.kenv/package.json

$ cd ~/.kit [08:27:27.980] [info] at async file:///~/.kenv/scripts/generate-graph.js:5:16 at kenvImport (file:///~/.kit/api/npm.js:77:15) the module (for instance, using npm rebuild or npm install). [113. Please try re-compiling or re-installing [108. This version of Node.js requires was compiled against a different Node.js version using [08:27:27.978] [warn] Error: Error: The module '~/.kenv/node_modules/canvas/build/Release/canvas.node'

Do they run different?

johnlindquist commented 1 year ago

@vhmartinezm I'll get this sorted in this week's build.

It's a matter of the script running from the app's node vs the ~/.knode/bin/node.

I made the change to run on the app's node because Windows would show a spinner any time your launch ~/.knode/bin/node. I may end up adding a metadata like below to force the app to use knode:

// Node: ~/.kit/bin/node

Not sure yet, but it ll be fixed soon.

vhmartinezm commented 1 year ago

@johnlindquist thank you for your answer. I like the metadata definition. I'll be waiting

vhmartinezm commented 1 year ago

Hey @johnlindquist! Any news on this topic in the latest version (1.53.22)?

johnlindquist commented 1 year ago

@vhmartinezm Should be sorted 👍

johnlindquist commented 1 year ago

@vhmartinezm Testing with this:

Open testing-node-canvas in Script Kit

// Name: Testing node-canvas

import "@johnlindquist/kit"

let { createCanvas, loadImage } = await import("canvas")

let canvas = createCanvas(200, 200)
let ctx = canvas.getContext("2d")

// download script kit logo
let buffer = await download(
  `https://d2eip9sf3oo6c2.cloudfront.net/playlists/square_covers/000/533/034/thumb/script-kit_2x.png`
)

let image = await loadImage(buffer)

ctx.rotate(0.1)
ctx.drawImage(image, 0, 0, 200, 200)

let filePath = tmpPath("canvas.png")
await writeFile(filePath, canvas.toBuffer())

// open with preview
await exec(`open ${filePath}`)
vhmartinezm commented 1 year ago

@johnlindquist worked perfect, thanks!