How to import npm scoped packages with @ in Deno?

63 Views Asked by At

I am trying to import @whiskeysockets/baileys in Deno like this:

import * as a from "npm:@whiskeysockets/baileys";
console.log(a);

But it's showing the following error when I try to run deno run main.ts:

error: Error in @whiskeysockets/[email protected] parsing version requirement for dependency: libsignal@github:adiwajshing/libsignal-node

Invalid npm version requirement. Unexpected character.
  github:adiwajshing/libsignal-node
  ~

Caused by:
    0: Invalid npm version requirement. Unexpected character.
         github:adiwajshing/libsignal-node
         ~
    1: Unexpected character.
         github:adiwajshing/libsignal-node

I'm guessing that it's interpreting 'whiskeysockets' as version number instead of the scope namespace since it's followed by '@'? Not sure. How can I fix this and import it properly?

1

There are 1 best solutions below

1
guest271314 On

There are a couple ways to do this. One is using esm.sh

import * as a from "https://esm.sh/@whiskeysockets/baileys";

This is how I fetch GitHub repositories and write them to a node_modules folder in the current working directory, which you can do for the source code of the project in your question

async function installRepositoryFromGitHubToNodeModules(url) {
  return new Deno.Command("/bin/sh", {
    /*
    cd node_modules/.deno
    git clone "$1"
    cd ..
    ln -s  "`pwd`/.deno/$2" "`pwd`"
  */
    args: [
      "install_from_github.sh",
      url,
      url.split("/").pop(),
    ],
  }).output();
}

const { code, stdout, stderr } = await installRepositoryFromGitHubToNodeModules(
  "https://github.com/guest271314/wbn-sign-webcrypto",
);

console.log([stdout, stderr].map((result) => decoder.decode(result)));