denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.23k stars 5.41k forks source link

Relative import path not prefixed when importing unrar #21440

Open arthurwolf opened 1 year ago

arthurwolf commented 1 year ago

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

bartlomieju commented 1 year ago

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

arthurwolf commented 1 year ago

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)

bartlomieju commented 1 year ago

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

I meant "import map" :)

arthurwolf commented 1 year ago

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: @.***>

--

勇気とユーモア

codingiswhyicry commented 9 months ago

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.

dsherret commented 6 months ago

Sorry, I closed the wrong issue.

dsherret commented 6 months ago

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

codingiswhyicry commented 6 months ago

Thanks so much :-)

dsherret commented 6 months ago

@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?)

Jamesernator commented 6 months ago

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.

MrMarble commented 1 month ago

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/fresh@1.7.2/",
    "$std/": "https://deno.land/std@0.216.0/",
    "@neondatabase/serverless": "npm:@neondatabase/serverless@^0.9.5",
    "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
    "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
    "drizzle-orm": "npm:drizzle-orm@^0.33.0",
    "postgres": "npm:postgres@^3.4.4",
    "preact": "https://esm.sh/preact@10.22.0",
    "preact/": "https://esm.sh/preact@10.22.0/",
    "react-masonry-component": "https://esm.sh/react-masonry-component@6.3.0",
    "tailwindcss": "npm:tailwindcss@3.4.1",
    "tailwindcss/": "npm:/tailwindcss@3.4.1/",
    "tailwindcss/plugin": "npm:/tailwindcss@3.4.1/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/preact-render-to-string@6.3.1/X-ZS8q/denonext/preact-render-to-string.mjs"
    at https://esm.sh/v135/preact-render-to-string@6.3.1/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?

dsherret commented 1 month ago

@MrMarble in workspaces the imports are scoped to the current package. The https://esm.sh/v135/preact-render-to-string@6.3.1/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/preact@10.22.0",
    "preact/": "https://esm.sh/preact@10.22.0/"
  }
}

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/preact@10.22.0",
    "preact/": "https://esm.sh/preact@10.22.0/"
  },
  // probably not necessary
  "scopes": {
    "https://esm.sh/": {
      "preact": "https://esm.sh/preact@10.22.0",
      "preact/": "https://esm.sh/preact@10.22.0/"
    }
  }
}
MrMarble commented 1 month ago

@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/preact@10.22.0",
    "preact/": "https://esm.sh/preact@10.22.0/",
    "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
    "tailwindcss": "npm:tailwindcss@3.4.1",
    "tailwindcss/": "npm:/tailwindcss@3.4.1/",
    "tailwindcss/plugin": "npm:/tailwindcss@3.4.1/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/fresh@1.7.2/",
    "$std/": "https://deno.land/std@0.216.0/",
    "@neondatabase/serverless": "npm:@neondatabase/serverless@^0.9.5",
    "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
    "drizzle-orm": "npm:drizzle-orm@^0.33.0",
    "postgres": "npm:postgres@^3.4.4",
    "react-masonry-component": "https://esm.sh/react-masonry-component@6.3.0"
  },
}
$ 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/fresh@1.7.2/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/signals@1.2.2/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/signals@1.2.2/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/signals@1.2.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/fresh@1.7.2/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/esbuild@v0.20.2/mod.js:1626:15)
    at https://deno.land/x/esbuild@v0.20.2/mod.js:1034:25
    at runOnEndCallbacks (https://deno.land/x/esbuild@v0.20.2/mod.js:1461:45)
    at buildResponseToResult (https://deno.land/x/esbuild@v0.20.2/mod.js:1032:7)
    at https://deno.land/x/esbuild@v0.20.2/mod.js:1061:16
    at responseCallbacks.<computed> (https://deno.land/x/esbuild@v0.20.2/mod.js:679:9)
    at handleIncomingPacket (https://deno.land/x/esbuild@v0.20.2/mod.js:739:9)
    at readFromStdout (https://deno.land/x/esbuild@v0.20.2/mod.js:655:7)
    at https://deno.land/x/esbuild@v0.20.2/mod.js:1974:11
    at eventLoopTick (ext:core/01_core.js:175:7)

what do I need to change now?

dsherret commented 1 month ago

@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.

und-miller commented 1 month ago

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/astro@4.16.4/node_modules/astro/dist/core/build/generate.js:42:16)
    at async AstroBuilder.build (file:///C:/Users/user.name/Source/project/node_modules/.deno/astro@4.16.4/node_modules/astro/dist/core/build/index.js:149:5)
    at async build (file:///C:/Users/user.name/Source/project/node_modules/.deno/astro@4.16.4/node_modules/astro/dist/core/build/index.js:51:3)
    at async runCommand (file:///C:/Users/user.name/Source/project/node_modules/.deno/astro@4.16.4/node_modules/astro/dist/cli/index.js:147:7)
AndryHTC commented 1 month ago

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

marvinhagemeister commented 1 month ago

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

MrMarble commented 1 month ago

@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

wesbos commented 1 month ago

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.

dsherret commented 1 month ago

@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

wesbos commented 1 month ago

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 commented 1 month ago

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!