Skip to content

/errors/deno/import-not-a-dependency

Deno: error: Import "chalk" not a dependency

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Deno
exit code
1
versions
Deno 2.x
os
any

error: Import "chalk" not a dependency means the code imports a bare name (no ./, no URL, no npm: or jsr: prefix) and Deno found no mapping for it in deno.json or package.json. Deno does not search a global module folder. Run deno add npm:chalk, which records the package in the imports field of deno.json, or write the import as import chalk from "npm:chalk". Deno 1.x printed the same problem as Relative import path "chalk" not prefixed with / or ./ or ../.

Reproduce it

bash
cat > main.ts <<'EOF'
import isOdd from "is-odd";
console.log(isOdd(3));
EOF
npx --yes deno@2 run main.ts

Output · exit 1

error: Import "is-odd" not a dependency
  hint: If you want to use the npm package, try running `deno add npm:is-odd`
    at file:///tmp/work/main.ts:1:19

The hint: line suggests the deno add command for the npm package with the same name. Check that the name is what you meant: Deno cannot tell an npm package from a JSR package or a typo.

Fix 1: map the name in deno.json

deno add (for a JSR package, deno add jsr:@std/path) creates or edits deno.json:

bash
cat > main.ts <<'EOF'
import isOdd from "is-odd";
console.log(isOdd(3));
EOF
npx --yes deno@2 add npm:is-odd@3.0.1 2>&1 | grep -v Download
cat deno.json
npx --yes deno@2 run main.ts

Output

Created deno.json configuration file.
Add npm:is-odd@3.0.1

Dependencies:
+ npm:is-odd 3.0.1

{
  "imports": {
    "is-odd": "npm:is-odd@3.0.1"
  }
}
true

The bare import import isOdd from "is-odd" now resolves, and the version lives in one place.

Fix 2: use an explicit specifier

bash
cat > main.ts <<'EOF'
import isOdd from "npm:is-odd@3.0.1";
console.log(isOdd(3));
EOF
npx --yes deno@2 run main.ts

Output

Download https://registry.npmjs.org/is-odd
Download https://registry.npmjs.org/is-number
Download https://registry.npmjs.org/is-odd/-/is-odd-3.0.1.tgz
Download https://registry.npmjs.org/is-number/-/is-number-6.0.0.tgz
true
SpecifierResolves to
npm:chalk@5The npm registry package, downloaded into the Deno cache
jsr:@std/path@1A package from the JSR registry
./lib/util.tsA local file (extension required)
https://example.org/mod.tsA remote module (needs --allow-import for most hosts)
chalkOnly what deno.json "imports" or package.json maps it to

Projects migrated from Node.js can keep a package.json with dependencies: Deno 2 reads it and resolves bare imports from it after deno install.

Sources