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

Cannot import module vscode-uri in a way that works for both jiti and Node.JS #105

Open
Litarvan opened this issue Dec 28, 2022 · 0 comments

Comments

@Litarvan
Copy link

Environment

  System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 578.78 MB / 32.00 GB
    Shell: 3.5.1 - ~/.nix-profile/bin/fish
  Binaries:
    Node: 18.8.0 - ~/.nix-profile/bin/node
    npm: 9.2.0 - ~/Library/pnpm/npm
  npmPackages:
    jiti: ^1.16.0 => 1.16.0
    vscode-uri: ^3.0.7 => 3.0.7

Reproduction

package.json:

{
  "type": "module",
  "dependencies": {
    "jiti": "^1.16.0",
    "vscode-uri": "^3.0.7"
  }
}

worksWithNode.js:

import vscodeUri from 'vscode-uri';
const { URI } = vscodeUri;

console.log(URI);

worksWithJiti.js:

import { URI } from 'vscode-uri';

console.log(URI);

Describe the bug

I can't find a way for the vscode-uri module to be loadable by both NodeJS and Jiti, I think that the fact it cannot load the module in the worksWithNode.js file is probably a bug.

Additional context

Even if it is not a bug, this behavior prevents me from using vite-plugin-checker in Nuxt 3 since it depends on vscode-uri and loads it like I did in the worksWithNode.js file. Nuxt will try to load it both with NodeJS and Jiti, thus giving me an error whatever I do when I try fixing the plugin.

I'm not the only one with this error, an issue has been opened on the vite-plugin-checker repository, but I see nothing wrong with what they are doing and the issue only happens when using Jiti, which makes me think the issue comes from your side.

For now, my workaround is to patch the plugin to load vscode-uri using the _require made with createRequire but I don't think this is really legit.

Logs

$ node worksWithJiti.js
file:///reproduction/worksWithJiti.js:1
import { URI } from 'vscode-uri';
         ^^^
SyntaxError: Named export 'URI' not found. The requested module 'vscode-uri' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'vscode-uri';
const { URI } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.8.0
$ node worksWithNode.js
[Function: t] {
  isUri: [Function (anonymous)],
  parse: [Function (anonymous)],
  file: [Function (anonymous)],
  from: [Function (anonymous)],
  revive: [Function (anonymous)]
}
$ node_modules/.bin/jiti worksWithJiti.js
[Function: t] {
  isUri: [Function (anonymous)],
  parse: [Function (anonymous)],
  file: [Function (anonymous)],
  from: [Function (anonymous)],
  revive: [Function (anonymous)]
}
$ node_modules/.bin/jiti worksWithNode.js
reproduction/node_modules/jiti/lib/index.js:2
  throw err /* ↓ Check stack trace ↓ */
  ^

TypeError: Cannot destructure property 'URI' of '_vscodeUri.default' as it is undefined.
    at /reproduction/worksWithNode.js:2:9
    at jiti (/reproduction/node_modules/jiti/dist/jiti.js:1:247502)
    at Object.<anonymous> (/reproduction/node_modules/jiti/bin/jiti.js:16:1)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:18:47

Node.js v18.8.0
$
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

No branches or pull requests

1 participant