deno icon indicating copy to clipboard operation
deno copied to clipboard

Deno add - "Failed to install from package.json" "Unexpected character."

Open silverbucket opened this issue 1 year ago • 5 comments

Version: Deno 2.0.4

$ deno add npm:debug
Add npm:[email protected]
error: Failed to install from package.json

Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         ^
         ~
$ cat deno.json
{
  "name": "...",
  "description": "...",
  "version": "3.0.0-alpha.5",
  "exports": "./src/index.ts",
  "tasks": {
    "build": "deno run --allow-read --allow-write scripts/export-json-schema.ts",
    "coverage": "deno coverage | deno run jsr:@silverbucket/threshold 100 88",
    "test": "deno test --coverage --clean --allow-env src/**/*.test.ts"
  },
  "imports": {
    "@silverbucket/threshold": "jsr:@silverbucket/[email protected]",
    "@types/node": "npm:@types/[email protected]",
    "@types/debug": "npm:@types/[email protected]",
    "ajv": "npm:[email protected]",
    "ajv-formats": "npm:[email protected]",
    "@silverbucket/ajv-formats-draft2019": "jsr:@silverbucket/[email protected]",
    "debug": "npm:[email protected]"
  }
}

silverbucket avatar Oct 31 '24 00:10 silverbucket

Do you also have a package.json file in your project?

bartlomieju avatar Oct 31 '24 11:10 bartlomieju

I tried reproducing the problem and with provided deno.json and running deno add npm:debug I don't get any error. @silverbucket could you please provide more information about your project?

bartlomieju avatar Oct 31 '24 12:10 bartlomieju

@bartlomieju Sure what would you like to see? To answer your question, there is no package.json.

$ ls -al
total 40
drwxr-xr-x@  9 njenning  staff   288 Oct 31 01:26 .
drwxr-xr-x@ 15 njenning  staff   480 Aug 26 23:06 ..
-rw-r--r--@  1 njenning  staff  1077 Aug 26 23:06 LICENSE
-rw-r--r--@  1 njenning  staff   271 Aug 29 14:54 README.md
drwxr-xr-x@  2 njenning  staff    64 Oct 31 01:24 coverage
-rw-r--r--@  1 njenning  staff   823 Oct 31 01:26 deno.json
-rw-r--r--@  1 njenning  staff  5814 Aug 27 17:53 deno.lock
drwxr-xr-x@  4 njenning  staff   128 Aug 27 17:44 scripts
drwxr-xr-x@ 12 njenning  staff   384 Oct 31 01:23 src

$ deno add npm:debug
Add npm:[email protected]
error: Failed to install from package.json

Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         ^
         ~

It might be worth mentioning that this is a mono-repo, however I've never had issues converting packages one at a time with deno 1.45, it was only upgrading to 2.0.x that I started getting issues like this.

silverbucket avatar Oct 31 '24 12:10 silverbucket

@silverbucket is your reo public so o could try it myself?

bartlomieju avatar Oct 31 '24 13:10 bartlomieju

We should improve this error message to say the file the error occurs in. Probably there's a package.json in the workspace in this case.

dsherret avatar Oct 31 '24 13:10 dsherret

@bartlomieju Here's the repo + branch + package in the mono repo where I ran those commands: https://github.com/sockethub/sockethub/tree/migrate-to-deno/packages/schemas

But while I was checking the modified files to commit and update that branch before I wrote this message, I noticed that the root package.json was modified with the debug dependencies. This goes along with what @dsherret was suggesting.

So, for some reason, using deno add while being in the packages/schemas/ directory (which has it's own deno.json file), deno is decided to try to add those to the repository root package.json and getting a syntax error of some sort (I still don't know what the syntax error is referring to as it's rather obtuse).

diff --git a/package.json b/package.json
index b805b00b..257b9dae 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,9 @@
   "engines": {
     "deno": ">=1.45"
   },
+  "dependencies": {
+    "debug": "^4.3.7"
+  },
   "devDependencies": {
     "@sockethub/data-layer": "workspace:1.0.0-alpha.4",
     "@sockethub/schemas": "workspace:3.0.0-alpha.4",

silverbucket avatar Oct 31 '24 14:10 silverbucket

I opened https://github.com/denoland/deno/pull/26665 which should help reduce the confusion slightly in the future. It will say the package.json file and the dependency that caused the issue.

So, for some reason, using deno add while being in the packages/schemas/ directory (which has it's own deno.json file), deno is decided to try to add those to the repository root package.json and getting a syntax error of some sort (I still don't know what the syntax error is referring to as it's rather obtuse).

@nathanwhit could you take a look? I don't think it should be doing that.

dsherret avatar Oct 31 '24 14:10 dsherret

Deno prefers to add npm dependencies to package.json file over deno.json, but yeah it's a bug - it should only happen for package.json in the same directory as deno.json, it shouldn't walk up the file tree to find package.json.

bartlomieju avatar Oct 31 '24 14:10 bartlomieju

Using deno 2.0.6 I'm getting another variation of this bug. Same repo I mentioned about, this time it's not with the package.json at the root of the repository, but a different pacakge at the same level as the schemas package.

project-root/packages/schemas $ deno install
error: Failed to install '@sockethub/client'
    at file:///project-root/packages/examples/package.json

Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         ^
         ~

silverbucket avatar Nov 10 '24 22:11 silverbucket

@nathanwhit Should I create a new ticket? Or can you re-open this one?

silverbucket avatar Nov 12 '24 11:11 silverbucket

Hmm we're putting the dependency in the correct file now, but we error out because we validate all of the package.json files in the workspace when we cache the dependencies, and one of them has a version requirement (workspace:^) that we don't support.

I feel like maybe we should make workspace:^ not a hard error?

nathanwhit avatar Nov 14 '24 22:11 nathanwhit

@nathanwhit we should just support it, see https://github.com/denoland/deno/issues/26726

bartlomieju avatar Nov 14 '24 23:11 bartlomieju

Hey, Im facing a similar issue when upgrading from v1.146 to v2.

We have a backend monorepo and it uses import_map.json files to manage dependencies per sub module in our monorepo. We don't have a package.json (except our Next.js frontend which is still on node). we do have a deno.jsonc at the root level of our project. After upgrading Deno, we get these errors when we run or try to install:

# install
error: Error in @stripe/[email protected] parsing version requirement for dependency "react-dom": ">=16.8.0 && <=^19.0.0"
deno install
Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         >=16.8.0 && <=^19.0.0
         ~
# run
Running  service
Watcher Process started.
error: Could not find a matching package for 'npm:[email protected]' in the node_modules directory. Ensure you have all your JSR and npm dependencies listed in your deno.json or package.json, then run `deno install`. Alternatively, turn on auto-install by specifying `"nodeModulesDir": "auto"` in your deno.json file.

Watcher Process started.
error: [ERR_MODULE_NOT_FOUND] Cannot find module 'file:///Users/app/node_modules/type-fest/index.js' imported from 'file:///Users/app/@types/date.types.ts'
    at file:///Users/app/@types/date.types.ts:8:24

this is part of our import_map.json file:

{
  "imports": {
    "kysely": "https://esm.sh/[email protected]",
    "pg": "npm:[email protected]",
    "query-string": "https://esm.sh/[email protected]?dts",
    "remeda": "https://esm.sh/[email protected]?dts",
    "stripe": "https://esm.sh/[email protected]?dts",
    "type-fest": "npm:[email protected]",
    "zod": "https://deno.land/x/[email protected]/mod.ts"
  }
}

bombillazo avatar Dec 13 '24 17:12 bombillazo

"react-dom": ">=16.8.0 && <=^19.0.0"

image

This doesn't work the way they probably think.

> import semver from "npm:semver"
undefined
> semver.satisfies("21.0.0", ">=16.8.0 && <=^19.0.0", { loose: true })
true

I opened https://github.com/stripe/react-stripe-js/pull/555/files

A huge problem in npm dependencies is npm allows people to just put whatever even when it doesn't make sense. I'll update deno_semver to handle that situation. Follow https://github.com/denoland/deno_semver/issues/41 for updates

I think though the original issue can now be closed because workspace specifiers like workspace:^ are now implemented https://github.com/denoland/deno/issues/26726

dsherret avatar Dec 13 '24 17:12 dsherret