deno icon indicating copy to clipboard operation
deno copied to clipboard

Relative import path not prefixed when importing unrar

Open arthurwolf opened this issue 1 year ago • 22 comments

Version: latest

After a full day banging my head against walls trying to get ESM to work and failing, somebody recommended I try deno.

I just tried, and it does this:

run --check backend/index.ts
error: Relative import path "unrar" not prefixed with / or ./ or ../
    at file:///home/arthur/dev/ai/manga/ui/backend/index.ts:17:34

which is weird that it would fail like this, thinking unrar is a relative import when it seemingly had no problem with the 10 other modules before that:

import express from 'express';
import multer from 'multer';
import cors from 'cors';
import fs from 'fs';
import {fileTypeFromBuffer} from 'file-type';
import {readChunk} from 'read-chunk';
import path from 'path';
import {glob} from 'glob';
// Database.
import sequelize from '../../src/db/database';
import Book from '../../src/db/book';
import Page from '../../src/db/page';

// Zip and rar stuff.
import unzipper from 'unzipper';
import tar from 'tar';
import { Extract as Unrar } from 'unrar';

console.log("z");

what's going on here?

thanks

arthurwolf avatar Dec 03 '23 03:12 arthurwolf

Can you share configuration files (Deno.json/package.json) and/or import mao files you have in your repository?

bartlomieju avatar Dec 03 '23 12:12 bartlomieju

Can you share configuration files (Deno.json/package.json)

This is the package.json in the folder I'm trying to run:

{
  "name": "manga-ui",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "serve": "vite",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start-backend": "nodemon --watch backend --exec ts-node
backend/index.ts",
    "dev": "concurrently \"npm run serve\" \"npm run start-backend\"",
    "note": "run the dev environment by typing npm run dev"
  },
  "dependencies": {
    ***@***.***/font": "7.0.96",
    ***@***.***/express": "^4.17.21",
    "adm-zip": "^0.5.10",
    "core-js": "^3.29.0",
    "file-type": "^18.7.0",
    "glob": "^10.3.10",
    "read-chunk": "^4.0.3",
    "roboto-fontface": "*",
    "sequelize": "^6.35.1",
    "sqlite3": "^5.1.6",
    "tar": "^6.2.0",
    "ts-node": "^10.9.1",
    "tsc": "^2.0.4",
    "tsx": "^4.6.2",
    "unrar": "^0.2.0",
    "unzipper": "^0.10.14",
    "vue": "^3.2.0",
    "vue-router": "^4.0.0",
    "vuetify": "^3.0.0"
  },
  "devDependencies": {
    ***@***.***/types": "^7.21.4",
    ***@***.***/node16": "^16.1.1",
    ***@***.***/node": "^18.19.1",
    ***@***.***/sequelize": "^4.28.19",
    ***@***.***/plugin-vue": "^4.0.0",
    ***@***.***/cli-service": "^5.0.8",
    ***@***.***/eslint-config-typescript": "^11.0.0",
    "eslint": "^8.22.0",
    "eslint-plugin-vue": "^9.3.0",
    "sass": "^1.60.0",
    "typescript": "^5.3.2",
    "unplugin-fonts": "^1.0.3",
    "vite": "^4.2.0",
    "vite-plugin-vuetify": "^1.0.0",
    "vue-tsc": "^1.2.0"
  }
}

and/or import mao files you have in your repository?

not sure what a mao file is (and Google wasn't very helpful about that)

arthurwolf avatar Dec 03 '23 12:12 arthurwolf

not sure what a mao file is (and Google wasn't very helpful about that)

I meant "import map" :)

bartlomieju avatar Dec 03 '23 12:12 bartlomieju

I don't think I have any of those, unless I'm missing something.

On Sun, Dec 3, 2023 at 1:35 PM Bartek Iwańczuk @.***> wrote:

not sure what a mao file is (and Google wasn't very helpful about that)

I meant "import map" :)

— Reply to this email directly, view it on GitHub https://github.com/denoland/deno/issues/21440#issuecomment-1837467609, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2SFJNEXGNQZWFLTQKDZ3YHRWXZAVCNFSM6AAAAABAEPKVA6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZXGQ3DONRQHE . You are receiving this because you authored the thread.Message ID: @.***>

--

勇気とユーモア

arthurwolf avatar Dec 03 '23 12:12 arthurwolf

Experiencing this after an upgrade to deno 1.39.1, which no longer has support for import maps (I think). Error started occurring after changing from import maps -> content in root level deno.json file.

codingiswhyicry avatar Feb 26 '24 10:02 codingiswhyicry

Sorry, I closed the wrong issue.

dsherret avatar May 13 '24 19:05 dsherret

@codingiswhyicry see issue https://github.com/denoland/deno/issues/23057 for that one, which is unrelated to this (it's fixed now)

dsherret avatar May 13 '24 19:05 dsherret

Thanks so much :-)

codingiswhyicry avatar May 13 '24 19:05 codingiswhyicry

@arthurwolf I tried running that code sample and there's a lot of missing dependencies not defined in the package.json like cors, multer, express. When I try to run:

import { Extract as Unrar } from 'unrar';
console.log("z");

With a package.json like:

{
  "dependencies": {
    "unrar": "^0.2.0"
  }
}

I get:

error: Uncaught SyntaxError: The requested module 'unrar' does not provide an export named 'Extract'
import { Extract as Unrar } from 'unrar';
         ^

Looking at that package, it doesn't seem to provide an Extract export.

Would you be able to provide a fully reproducible example of the error you're encountering? (an example without redactions that anyone could run easily to reproduce the problem?)

dsherret avatar May 13 '24 19:05 dsherret

I am getting this also when importing in the REPL, the cause is importing a node-style module from outside of it's package, i.e. if we have:

base/
  example-package/
     node_modules/
     package.json
     example.js

If while in base/ we import from that package (where example.js imports glob) in the repl I get the error:

% deno
Deno 1.43.5
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> await import("./example-package/example.js")
Uncaught TypeError: Relative import path "glob" not prefixed with / or ./ or ../
    at file:///home/jamesernator/projects/example-package/example.js:1:22
    at async <anonymous>:1:22

The error doesn't happen with deno run --allow-all ./example-package/example.js, so I'm not sure exactly what is causing it.

Jamesernator avatar May 21 '24 17:05 Jamesernator

I'm also facing this issue using deno workspaces with deno 2.0 My package was running fine on its own, but after moving it to a workspace I can't run any task as all fail with the same error

root deno.json

{
  "lock": true,
  "workspace": [
    "packages/www"
  ],
  "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
  "nodeModulesDir": "auto"
}

www deno.json

{
  "tasks": {
    "check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
    "cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
    "manifest": "deno task cli manifest $(pwd)",
    "start": "deno run -A --watch=static/,routes/ dev.ts",
    "build": "deno run -A dev.ts build",
    "preview": "deno run -A main.ts",
    "update": "deno run -A -r https://fresh.deno.dev/update ."
  },
  "lint": { "rules": { "tags": ["fresh", "recommended"] } },
  "exclude": ["**/_fresh/*"],
  "imports": {
    "$fresh/": "https://deno.land/x/[email protected]/",
    "$std/": "https://deno.land/[email protected]/",
    "@neondatabase/serverless": "npm:@neondatabase/serverless@^0.9.5",
    "@preact/signals": "https://esm.sh/*@preact/[email protected]",
    "@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
    "drizzle-orm": "npm:drizzle-orm@^0.33.0",
    "postgres": "npm:postgres@^3.4.4",
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/",
    "react-masonry-component": "https://esm.sh/[email protected]",
    "tailwindcss": "npm:[email protected]",
    "tailwindcss/": "npm:/[email protected]/",
    "tailwindcss/plugin": "npm:/[email protected]/plugin.js"
  }
}

running deno install creates the node_modules folder and the deno.lock file but trying to run any of the task defined on packages/www/deno.json will fail

$ cd ./packages/www/
$ deno task build   
Task build deno run -A dev.ts build
error: Relative import path "preact" not prefixed with / or ./ or ../ and not in import map from "https://esm.sh/v135/[email protected]/X-ZS8q/denonext/preact-render-to-string.mjs"
    at https://esm.sh/v135/[email protected]/X-ZS8q/denonext/preact-render-to-string.mjs:2:46

Removing the root deno.json file makes the package tasks work. Duplicating the import property from the package to the root also works, but why do I need to duplicate the imports if they are only used on the child package?

MrMarble avatar Oct 10 '24 07:10 MrMarble

@MrMarble in workspaces the imports are scoped to the current package. The https://esm.sh/v135/[email protected]/X-ZS8q/denonext/preact-render-to-string.mjs module uses a "preact" bare specifier. You might need to move the preact import up to the root of the workspace so that it applies everywhere:

{
  "lock": true,
  "workspace": [
    "packages/www"
  ],
  "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
  "nodeModulesDir": "auto",
  "imports": {
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/"
  }
}

Otherwise, try creating a scope for esm.sh to use that specific preact version:

{
  "lock": true,
  "workspace": [
    "packages/www"
  ],
  "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
  "nodeModulesDir": "auto",
  "imports": {
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/"
  },
  // probably not necessary
  "scopes": {
    "https://esm.sh/": {
      "preact": "https://esm.sh/[email protected]",
      "preact/": "https://esm.sh/[email protected]/"
    }
  }
}

dsherret avatar Oct 10 '24 08:10 dsherret

@dsherret I moved the preact imports and after that I got similar errors with other packages so I just moved them but now I'm getting some obscure error I don't fully understand

root deno.json

{
  "lock": true,
  "workspace": [
    "packages/www"
  ],
  "imports": {
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/",
    "@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
    "tailwindcss": "npm:[email protected]",
    "tailwindcss/": "npm:/[email protected]/",
    "tailwindcss/plugin": "npm:/[email protected]/plugin.js"
  },
  "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
  "nodeModulesDir": "auto"
}

www deno.json

{
  "tasks": {
    "check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
    "cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
    "manifest": "deno task cli manifest $(pwd)",
    "start": "deno run -A --watch=static/,routes/ dev.ts",
    "build": "deno run -A dev.ts build",
    "preview": "deno run -A main.ts",
    "update": "deno run -A -r https://fresh.deno.dev/update ."
  },
  "lint": { "rules": { "tags": ["fresh", "recommended"] } },
  "exclude": ["**/_fresh/*"],
  "imports": {
    "$fresh/": "https://deno.land/x/[email protected]/",
    "$std/": "https://deno.land/[email protected]/",
    "@neondatabase/serverless": "npm:@neondatabase/serverless@^0.9.5",
    "@preact/signals": "https://esm.sh/*@preact/[email protected]",
    "drizzle-orm": "npm:drizzle-orm@^0.33.0",
    "postgres": "npm:postgres@^3.4.4",
    "react-masonry-component": "https://esm.sh/[email protected]"
  },
}
$ cd ./packages/www
$ deno task build
Task build deno run -A dev.ts build
The manifest has been generated for 9 routes and 3 islands.
X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    islands/Filters.tsx:1:33:
      1 │ import { useRef, useState } from "preact/hooks";
        ╵                                  ~~~~~~~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://deno.land/x/[email protected]/src/runtime/entrypoints/main.ts:13:7:
      13 │ } from "preact";
         ╵        ~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    islands/Load.tsx:3:34:
      3 │ import { useEffect, useRef } from "preact/hooks";
        ╵                                   ~~~~~~~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://esm.sh/v135/@preact/[email protected]/X-ZS8q/denonext/signals.mjs:2:59:
      2 │ import{Component as y,options as v,isValidElement as S}from"preact";import{useMemo as c,useRef as h,useEffect as k}from"preact/hooks";import{Signal as m,computed a... 
        ╵                                                            ~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://esm.sh/v135/@preact/[email protected]/X-ZS8q/denonext/signals.mjs:2:119:
      2 │ ...ent as S}from"preact";import{useMemo as c,useRef as h,useEffect as k}from"preact/hooks";import{Signal as m,computed as b,signal as g,effect as p}from"@preact/si... 
        ╵                                                                             ~~~~~~~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://esm.sh/v135/@preact/[email protected]/X-ZS8q/denonext/signals.mjs:2:195:
      2 │ .../hooks";import{Signal as m,computed as b,signal as g,effect as p}from"@preact/signals-core";import{Signal as M,batch as N,computed as O,effect as P,signal as R,... 
        ╵                                                                         ~~~~~~~~~~~~~~~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://deno.land/x/[email protected]/src/runtime/head.ts:1:49:
      1 │ import { ComponentChildren, createContext } from "preact";
        ╵                                                  ~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://deno.land/x/[email protected]/src/runtime/csp.ts:1:30:
      1 │ import { createContext } from "preact";
        ╵                               ~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://deno.land/x/[email protected]/src/runtime/head.ts:2:27:
      2 │ import { useContext } from "preact/hooks";
        ╵                            ~~~~~~~~~~~~~~

X [ERROR] specifier was a bare specifier, but was not remapped to anything by importMap. [plugin deno-resolver]

    https://deno.land/x/[email protected]/src/runtime/csp.ts:2:27:
      2 │ import { useContext } from "preact/hooks";
        ╵                            ~~~~~~~~~~~~~~

error: Uncaught (in promise) Error: Build failed with 10 errors:
https://deno.land/x/[email protected]/src/runtime/csp.ts:1:30: ERROR: [plugin: deno-resolver] specifier was a bare specifier, but was not remapped to anything by importMap.
https://deno.land/x/[email protected]/src/runtime/csp.ts:2:27: ERROR: [plugin: deno-resolver] specifier was a bare specifier, but was not remapped to anything by importMap.
https://deno.land/x/[email protected]/src/runtime/entrypoints/main.ts:13:7: ERROR: [plugin: deno-resolver] specifier was a bare specifier, but was not remapped to anything by importMap.
https://deno.land/x/[email protected]/src/runtime/head.ts:1:49: ERROR: [plugin: deno-resolver] specifier was a bare specifier, but was not remapped to anything by importMap.
https://deno.land/x/[email protected]/src/runtime/head.ts:2:27: ERROR: [plugin: deno-resolver] specifier was a bare specifier, but was not remapped to anything by importMap.
...
  let error = new Error(text);
              ^
    at failureErrorWithLog (https://deno.land/x/[email protected]/mod.js:1626:15)
    at https://deno.land/x/[email protected]/mod.js:1034:25
    at runOnEndCallbacks (https://deno.land/x/[email protected]/mod.js:1461:45)
    at buildResponseToResult (https://deno.land/x/[email protected]/mod.js:1032:7)
    at https://deno.land/x/[email protected]/mod.js:1061:16
    at responseCallbacks.<computed> (https://deno.land/x/[email protected]/mod.js:679:9)
    at handleIncomingPacket (https://deno.land/x/[email protected]/mod.js:739:9)
    at readFromStdout (https://deno.land/x/[email protected]/mod.js:655:7)
    at https://deno.land/x/[email protected]/mod.js:1974:11
    at eventLoopTick (ext:core/01_core.js:175:7)

what do I need to change now?

MrMarble avatar Oct 10 '24 08:10 MrMarble

@MrMarble workspaces aren't supported in fresh yet (in this case, you may be able to work around it by duplicating the preact imports in both the workspace and workspace member). Follow https://github.com/lucacasonato/esbuild_deno_loader/pull/145 for updates and also look out for a fresh release that supports workspaces.

dsherret avatar Oct 10 '24 08:10 dsherret

I am also receiving similar errors when building my astro project:

Relative import path "clsx" not prefixed with / or ./ or ../ and not in import map from "file:///C:/Users/user.name/Source/project/packages/web/frontend/dist/server/manifest_BKOn2dZT.mjs?time=1729004711173"
    at file:///C:/Users/user.name/Source/project/packages/web/frontend/dist/server/manifest_BKOn2dZT.mjs?time=1729004711173:7:8
  Stack trace:
    at file:///C:/Users/user.name/Source/project/packages/web/frontend/dist/server/manifest_BKOn2dZT.mjs?time=1729004711173:7:8
    at async generatePages (file:///C:/Users/user.name/Source/project/node_modules/.deno/[email protected]/node_modules/astro/dist/core/build/generate.js:42:16)
    at async AstroBuilder.build (file:///C:/Users/user.name/Source/project/node_modules/.deno/[email protected]/node_modules/astro/dist/core/build/index.js:149:5)
    at async build (file:///C:/Users/user.name/Source/project/node_modules/.deno/[email protected]/node_modules/astro/dist/core/build/index.js:51:3)
    at async runCommand (file:///C:/Users/user.name/Source/project/node_modules/.deno/[email protected]/node_modules/astro/dist/cli/index.js:147:7)

und-miller avatar Oct 15 '24 15:10 und-miller

I could never use the deno.json file to set the import map, 1.x nor 2.x.

Tried again now to deploy Supabase Functions and only works if I put the imports into a file named import_map.json

AndryHTC avatar Oct 18 '24 09:10 AndryHTC

@MrMarble Does updating Fresh to 1.7.3 help? We landed some improvements to the esbuild deno loader.

marvinhagemeister avatar Oct 18 '24 12:10 marvinhagemeister

@marvinhagemeister I can confirm that with the same setup described above, running deno task build on my fresh package does work on 1.7.3

MrMarble avatar Oct 18 '24 14:10 MrMarble

Also hitting this trying to run a start script that imports an app from the parent. I'm just trying to run an existing node app to see how Deno 2.0.2 handles it.

app.ts in root dir contains the app

bin/start.ts starts the app and looks like this:

import app from '../app.ts'

deno run ./bin/start.ts from the root dir gives the error:

error: Relative import path "path" not prefixed with / or ./ or ../
  hint: If you want to use a built-in Node module, add a "node:" prefix (ex. "node:path").

Funny enough, if I change the import from ../app.ts to ./app.ts, the it finds the file - but that in incorrect.

Node runs it fine.

So there is some difference between where the app file is located and the directory you run it from?

I should probably just move my start file out of that sub-folder, but I thought I'd log the issue as it was a very common express.js pattern a while back and others may hit this.

wesbos avatar Oct 24 '24 16:10 wesbos

@wesbos it looks like Deno isn't surfacing where the error comes from for some reason, which is a bug (it should say at <url-goes-here>:<location> in the message).

hint: If you want to use a built-in Node module, add a "node:" prefix (ex. "node:path").

Probably running with --unstable-bare-node-builtins will make it work https://docs.deno.com/runtime/reference/cli/unstable_flags/ -- We've yet to stabilize that

dsherret avatar Oct 24 '24 16:10 dsherret

Oh sorry it does show me where the import error is coming from - it points right to the import app from '../app.ts' but it doesn't show me where that node API is being used - should it? It's just an import.

Ill try it with that flag

wesbos avatar Oct 24 '24 16:10 wesbos

I moved the file out of that nested folder and I'm working my way through adding node: prefix to builtins - seems I'm past this. Thanks!

wesbos avatar Oct 24 '24 16:10 wesbos