/errors/deno/import-not-a-dependency
Deno: error: Import "chalk" not a dependency
- 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
cat > main.ts <<'EOF'
import isOdd from "is-odd";
console.log(isOdd(3));
EOF
npx --yes deno@2 run main.tsOutput · 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:19The 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:
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.tsOutput
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"
}
}
trueThe bare import import isOdd from "is-odd" now resolves, and the version lives in one place.
Fix 2: use an explicit specifier
cat > main.ts <<'EOF'
import isOdd from "npm:is-odd@3.0.1";
console.log(isOdd(3));
EOF
npx --yes deno@2 run main.tsOutput
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
| Specifier | Resolves to |
|---|---|
| npm:chalk@5 | The npm registry package, downloaded into the Deno cache |
| jsr:@std/path@1 | A package from the JSR registry |
| ./lib/util.ts | A local file (extension required) |
| https://example.org/mod.ts | A remote module (needs --allow-import for most hosts) |
| chalk | Only 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.