google / zx

A tool for writing better scripts
https://google.github.io/zx/
Apache License 2.0
43.1k stars 1.09k forks source link

Autocomplete in VS Code - ERR_MODULE_NOT_FOUND #401

Closed mak1A4 closed 2 years ago

mak1A4 commented 2 years ago

Expected Behavior

Autocomplete for globals in vscode show up

Actual Behavior

I get an "ERR_MODULE_NOT_FOUND" error when executing a script which cointains import zx/globals

Steps to Reproduce the Problem

  1. Install zx globally either from npm or directly from github repo
  2. Execute the following script
    #!/usr/bin/env zx
    import 'zx/globals'
    console.log(1 + 5)

    For me this results in the following error:

    
    node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'zx' imported from /Users/myUser/Scripts/calc.mjs at new NodeError (node:internal/errors:372:5) at packageResolve (node:internal/modules/esm/resolve:908:9) at moduleResolve (node:internal/modules/esm/resolve:957:20) at defaultResolve (node:internal/modules/esm/resolve:1173:11) at ESMLoader.resolve (node:internal/modules/esm/loader:604:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:318:18) at ModuleWrap. (node:internal/modules/esm/module_job:80:40) at link (node:internal/modules/esm/module_job:78:36) { code: 'ERR_MODULE_NOT_FOUND' }


I'm not sure if this is a bug or I'm doing something wrong or missing something obvious.

## Specifications
- Platform: MacOS Apple Silicon (M1 Pro) - Node.js v17.9.0
antonmedv commented 2 years ago

Remove line: import 'zx/globals'

this is esm limitation.

mak1A4 commented 2 years ago

So there currently is no way to get autocompletion in vscode if zx is installed globally and not locally? If so, maybe update the Readme because there it's stated that this is how you should get autocompletion.

antonmedv commented 2 years ago

It’s possible use reference.

mak1A4 commented 2 years ago

Thanks this // <reference 'zx/globals' /> worked : )

kongmoumou commented 2 years ago

Thanks this // <reference 'zx/globals' /> worked : )

Hi, how did u use reference directive 😂? @mak1A4

I use like below but it said "Cannot find type definition file for 'zx/globals'.". Did u also install zx as a global module?

/// <reference types="zx/globals" />
bluebrown commented 2 years ago

I have the issue too. The readme should be fixed to say that its not possible. I also added // <reference 'zx/globals' /> but nothing happened.

kongmoumou commented 2 years ago

@bluebrown I installed zx in node module path($HOME in my case), then import { $ } from 'zx', and then the ts intellisense is working. You could have a try~

itwondersteam commented 2 years ago

@kongmoumou thanks, cd ~ && npm i zx and now it works