geelen / npx-import

https://npx-import.pages.dev/
161 stars 8 forks source link

Windows support #5

Closed geelen closed 2 years ago

geelen commented 2 years ago

Hard for me to test as I don't have a working windows env, sorry!

If someone can run npx -y -p is-odd node -e 'console.log(process.env.PATH.split(":"))' in their environment, I think NPX on windows is just using a different directory structure than I expect...

geelen commented 2 years ago

Diff showing how the PATH changes on windows running in npx: https://www.diffchecker.com/cbHUDAKU

Looks like '\\Users\\<username>\\AppData\\Local\\npm-cache\\_npx\\e1b5bd0eb9f99fbc\\node_modules\\.bin' is the row we're looking for, so we just need to change up the search in here: https://github.com/geelen/npx-import/blob/main/src/index.ts#L161

geelen commented 2 years ago

@Cyb3r-Jak3 could you give this a test on windows?

# new directory
npm init -y
npm install --save-dev npx-import@wip
node

Then within the dir

await npxImport('is-odd')

And let me know if it works? Sorry about this, I don't have any other good way of testing this...

Cyb3r-Jak3 commented 2 years ago
C:\Users\Jake> cd F:\git\npx-import\
F:\git\npx-import> npm init -y
Wrote to F:\git\npx-import\package.json:

{
  "name": "npx-import",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

F:\git\npx-import> npm install --save-dev npx-import@wip

added 24 packages, and audited 25 packages in 3s

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
F:\git\npx-import> node
Welcome to Node.js v16.13.1.
Type ".help" for more information.
> await npxImport('is-odd')
Uncaught ReferenceError: npxImport is not defined
    at REPL1:1:16
>

Didn't seem to work.

Version from package.json

  "devDependencies": {
    "npx-import": "^1.0.3-0"
  }
geelen commented 2 years ago

Lol, sorry, missed a line. Within the node CLI you should do

const npxImport = require('npx-import')
await npxImport('is-odd')

Or make a index.js file with

import npxImport from 'npx-import'
console.log(await npxImport('is-odd'))

and run that.

Cyb3r-Jak3 commented 2 years ago

Neither of those seems to work CLI

F:\git\npx-import> node
Welcome to Node.js v16.13.1.
Type ".help" for more information.
> const npxImport = require('npx-import')
Uncaught:
Error [ERR_REQUIRE_ESM]: require() of ES Module F:\git\npx-import\node_modules\npx-import\lib\index.js not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at new NodeError (node:internal/errors:371:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1128:19)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'ERR_REQUIRE_ESM'
}
>

Using index.js

F:\git\npx-import> cat .\index.js
import npxImport from 'npx-import'
console.log(await npxImport('is-odd'))
F:\git\npx-import> node --trace-warnings .\index.js
file:///F:/git/npx-import/index.js:1
import npxImport from 'npx-import'
       ^^^^^^^^^
SyntaxError: The requested module 'npx-import' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)
geelen commented 2 years ago

Lol I should really confirm that these snippets work before sending them to you, sorry 😬

Try in node (assuming npx-import@wip is already installed locally):

const { npxImport } = await import('npx-import')
await npxImport('is-odd')

Should look like this:

image

Or a index.mjs file (note, .mjs not .js):

import { npxImport } from 'npx-import'
console.log(await npxImport('is-odd'))

image

Appreciate the help here. Sorry for the broken snippets..

Cyb3r-Jak3 commented 2 years ago

Snippets work but still seeing errors

F:\git\npx-import> npm install --save-dev npx-import@wip

up to date, audited 25 packages in 811ms

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
F:\git\npx-import> node
Welcome to Node.js v16.13.1.
Type ".help" for more information.
> const { npxImport } = await import('npx-import')
undefined
> await npxImport('is-odd')
[NPXI] is-odd not available locally. Attempting to use npx to install temporarily.
[NPXI] Installing... (npx --prefer-online -y -p is-odd@latest)
Uncaught Error: npx-import failed for is-odd with message:
    Failed to find temporary install directory. Looking for paths matching '/.npm/_npx/' in:
[""]

You should install is-odd locally:
    npm install --save-dev is-odd@latest

    at npxImport (file:///F:/git/npx-import/node_modules/npx-import/lib/index.js:28:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async REPL2:1:24
>
F:\git\npx-import> cat .\index.mjs
import { npxImport } from 'npx-import'
console.log(await npxImport('is-odd'))
F:\git\npx-import> node .\index.mjs
[NPXI] is-odd not available locally. Attempting to use npx to install temporarily.
[NPXI] Installing... (npx --prefer-online -y -p is-odd@latest)
file:///F:/git/npx-import/node_modules/npx-import/lib/index.js:28
            throw new Error(`npx-import failed for ${missingPackages
                  ^

Error: npx-import failed for is-odd with message:
    Failed to find temporary install directory. Looking for paths matching '/.npm/_npx/' in:
[""]

You should install is-odd locally:
    npm install --save-dev is-odd@latest

    at npxImport (file:///F:/git/npx-import/node_modules/npx-import/lib/index.js:28:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async file:///F:/git/npx-import/index.mjs:2:13

It doesn't seem to like the temp directory searching. On yours it goes to the user's home directory but on mine it seems to go to / which doesn't exist on Windows. There appears to be no ~/.npm folder on Windows.

geelen commented 2 years ago

Ok damn, i'm going to need to get myself a windows dev env to fix this. I need to understand what process.env.PATH looks like in windows...

Cyb3r-Jak3 commented 2 years ago

Here is a dump of mine.

{
  ALLUSERSPROFILE: 'C:\\ProgramData',
  APPDATA: 'C:\\Users\\Jake\\AppData\\Roaming',
  'asl.log': 'Destination=file',
  ChocolateyInstall: 'C:\\ProgramData\\chocolatey',
  ChocolateyLastPathUpdate: '133010953757205201',
  ChocolateyToolsLocation: 'C:\\tools',
  CommonProgramFiles: 'C:\\Program Files\\Common Files',
  'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
  CommonProgramW6432: 'C:\\Program Files\\Common Files',
  ComSpec: 'C:\\WINDOWS\\system32\\cmd.exe',
  CUDA_PATH: 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4',
  CUDA_PATH_V11_4: 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4',
  DriverData: 'C:\\Windows\\System32\\Drivers\\DriverData',
  GoLand: 'F:\\Program Files\\JetBrains\\GoLand 2021.1.1\\bin;',
  GOPATH: 'F:\\go\\path',
  HOME: 'C:\\Users\\Jake',
  HOMEDRIVE: 'C:',
  HOMEPATH: '\\Users\\Jake',
  INTEL_DEV_REDIST: 'C:\\Program Files (x86)\\Common Files\\Intel\\Shared Libraries\\',
  JD2_HOME: 'F:\\Program Files\\JDownloader',
  LOCALAPPDATA: 'C:\\Users\\Jake\\AppData\\Local',
  LOGONSERVER: '\\\\JAKE-PC',
  MIC_LD_LIBRARY_PATH: 'C:\\Program Files (x86)\\Common Files\\Intel\\Shared Libraries\\compiler\\lib\\mic',
  NUMBER_OF_PROCESSORS: '16',
  NVCUDASAMPLES11_4_ROOT: 'C:\\ProgramData\\NVIDIA Corporation\\CUDA Samples\\v11.4',
  NVCUDASAMPLES_ROOT: 'C:\\ProgramData\\NVIDIA Corporation\\CUDA Samples\\v11.4',
  NVTOOLSEXT_PATH: 'C:\\Program Files\\NVIDIA Corporation\\NvToolsExt\\',
  OneDrive: 'C:\\Users\\Jake\\OneDrive',
  OPENSSL_DIR: 'F:\\Program Files\\OpenSSL-Win64',
  OS: 'Windows_NT',
  Path: 'F:\\Program Files (x86)\\VMware\\bin\\;F:\\Program Files\\Python310\\Scripts\\;F:\\Program Files\\Python310\\;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4\\libnvvp;C:\\Program Files\\Common Files\\Oracle\\Java\\javapath;C:\\Python39\\Scripts\\;C:\\Python39\\;F:\\Program Files\\Python37\\Scripts\\;F:\\Program Files\\Python37\\;F:\\Program Files\\Python 3.7.3\\Scripts\\;F:\\Program Files\\Python 3.7.3\\;C:\\Program Files (x86)\\Common Files\\Intel\\Shared Libraries\\redist\\intel64\\compiler;E:\\Python37\\Scripts\\;E:\\Python37\\;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;F:\\Program Files (x86)\\Calibre\\;C:\\ProgramData\\chocolatey\\bin;F:\\Program Files\\Microsoft VS Code\\bin;F:\\Program Files\\Putty\\;C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\Binn\\;F:\\Program Files\\Calibre2\\;F:\\Program Files\\AWS_CLI\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;F:\\Program Files (x86)\\Google\\Cloud SDK\\google-cloud-sdk\\bin;C:\\Program Files\\dotnet\\;F:\\Program Files\\GCC\\bin;F:\\Program Files\\PowerShell\\7\\;C:\\Program Files\\NVIDIA Corporation\\Nsight Compute 2021.2.1\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;F:\\Program Files\\Go\\bin;F:\\Program Files\\nodejs\\;F:\\Program Files\\FileBot\\;F:\\Program Files (x86)\\Gpg4win\\..\\GnuPG\\bin;F:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\ZeroTier\\One\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Users\\Jake\\AppData\\Local\\Programs\\Python\\Launcher\\;F:\\Ruby30-x64\\bin;C:\\Users\\Jake\\.cargo\\bin;C:\\Users\\Jake\\Downloads\\google-cloud-sdk\\bin;C:\\Users\\Jake\\AppData\\Local\\Microsoft\\WindowsApps;F:\\rclone;F:\\Program Files\\ffmpeg\\bin;C:\\Users\\Jake\\AppData\\Roaming\\Python\\Python37\\Scripts;F:\\Program Files (x86)\\Nmap;F:\\Program Files\\heroku\\bin;F:\\Program Files\\PostgreSQL\\12\\bin;F:\\Program Files (x86)\\FAHClient;C:\\Users\\Jake\\AppData\\Local\\Programs\\orca;C:\\Users\\Jake\\AppData\\Local\\Microsoft\\WindowsApps;F:\\Program Files\\JetBrains\\PyCharm 2020.2\\bin;F:\\Program Files (x86)\\Pypy3;F:\\Program Files\\Python38\\Scripts;F:\\Program Files\\JetBrains\\GoLand 2021.1.1\\bin;F:\\go\\1.17.2\\bin;F:\\Cool Stuff\\Exes;C:\\Users\\Jake\\go\\bin;C:\\Users\\Jake\\AppData\\Roaming\\npm;C:\\Users\\Jake\\AppData\\Local\\Keybase\\',
  PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW;.RB;.RBW;.CPL',
  POWERSHELL_DISTRIBUTION_CHANNEL: 'MSI:Windows 10 Pro',
  PROCESSOR_ARCHITECTURE: 'AMD64',
  PROCESSOR_IDENTIFIER: 'AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD',
  PROCESSOR_LEVEL: '23',
  PROCESSOR_REVISION: '0802',
  ProgramData: 'C:\\ProgramData',
  ProgramFiles: 'C:\\Program Files',
  'ProgramFiles(x86)': 'C:\\Program Files (x86)',
  ProgramW6432: 'C:\\Program Files',
  PSModulePath: 'C:\\Users\\Jake\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules;C:\\Program Files\\Intel\\Wired Networking\\',
  PT7HOME: 'F:\\Program Files\\Cisco Packet Tracer 7.3.0',
  PUBLIC: 'C:\\Users\\Public',
  QT_DEVICE_PIXEL_RATIO: 'auto',
  SystemDrive: 'C:',
  SystemRoot: 'C:\\WINDOWS',
  TEMP: 'C:\\Users\\Jake\\AppData\\Local\\Temp',
  TMP: 'C:\\Users\\Jake\\AppData\\Local\\Temp',
  USERDOMAIN: 'JAKE-PC',
  USERDOMAIN_ROAMINGPROFILE: 'JAKE-PC',
  USERNAME: 'Jake',
  USERPROFILE: 'C:\\Users\\Jake',
  VBOX_MSI_INSTALL_PATH: 'C:\\Program Files\\Oracle\\VirtualBox\\',
  VS140COMNTOOLS: 'F:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\',
  windir: 'C:\\WINDOWS',
  WSLENV: 'WT_SESSION::WT_PROFILE_ID',
}
geelen commented 2 years ago

Fixed in release v1.1.0