最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to add dev-only dependencies in Deno? - Stack Overflow

matteradmin5PV0评论

In Node.js, we can use npm install -D foo to add a dependency into package.json > devDependencies. When the package is published and used as a dependency, devDependencies won't be installed.

But in deno.json, there is only imports, no devImports. According to Deno v2 RC announcement, we can use deno add --dev. But this only add contents to deno.lock, not adding anything to deno.json. I tried deno add --dev eslint (not migrating Deno Lint yet), but it complained eslint: command not found.

When package.json exists, it's added to package.json, but I'm trying finding a pure-Deno solution.

I found denoland/deno#26865, but no answers are there.

In Node.js, we can use npm install -D foo to add a dependency into package.json > devDependencies. When the package is published and used as a dependency, devDependencies won't be installed.

But in deno.json, there is only imports, no devImports. According to Deno v2 RC announcement, we can use deno add --dev. But this only add contents to deno.lock, not adding anything to deno.json. I tried deno add --dev eslint (not migrating Deno Lint yet), but it complained eslint: command not found.

When package.json exists, it's added to package.json, but I'm trying finding a pure-Deno solution.

I found denoland/deno#26865, but no answers are there.

Share Improve this question edited Nov 20, 2024 at 12:34 typed-sigterm asked Nov 17, 2024 at 12:19 typed-sigtermtyped-sigterm 1,24711 silver badges35 bronze badges 3
  • "…install a dev-only dependency": Can you edit the question to clarify what is meant by "install"? Are you referring to the specific scenario of using npm dependencies while using package.json and a local node_modules directory? By default, Deno downloads imported module data into an immutable, global cache. Read more at the docs: Modules and dependencies – jsejcksn Commented Nov 19, 2024 at 23:18
  • @jsejcksn Edited. I'm just finding a thing in Deno like devDependencies in Node.js/npm – typed-sigterm Commented Nov 20, 2024 at 12:38
  • 1 Looks like there is no option to add a package as a dev dependency for packages published on JSR. The --dev flag works only for packages published on npm and requires package.json file. Find out more information in the reply of the GitHub issue you have linked: github/denoland/deno/issues/26865#issuecomment-2480833535 – Kotkoroid Commented Nov 21, 2024 at 16:58
Add a comment  | 

1 Answer 1

Reset to default 4

See this answer by "crowKats" here, but in short:

With a “npm-style” Deno project, based on package.json, use

  • deno add --dev for dev dependencies, or
  • deno add for production dependencies.

But with a deno.json or deno.jsonc project, use

  • deno add, in either case.

There’s no need to segregate development and production dependencies for Deno 2+ native projects. Deno is smart: it only caches and installs the dependencies needed for the current runtime.

While I’m not a Deno expert myself (still learning), my suggestion is that you need to read Deno's documentation attentively but also with an "open mind" (don't expect it to just be a cuter, giant-lizard version of npm):

  • In the npm world, npm install is required before running any project, otherwise the application will break: dependencies wont't be available in node_modules when some code tries to access it.

  • With Deno-style though, there is no node_modules; nevertheless, dependencies will also be cached automatically at runtime. You can (and should, especially in production) run deno cache ahead of time, of course - so you can prepopulate the cache with all identified packages, before they are used. But if you, say, add a new dependency or make a hotfix change in production, Deno's expected behaviour is to detect it, automatically download the dependency to its cache, and continue executing the application without further interruption.

  • To make things even weirder, this also means not even deno add is really required. According to Deno's docs, imports in the code should be "unequivocal": e.g. they must specify the package registry or source - with the npm:, node: or jsr: prefixes, for example. And this "extra" info is enough for Deno's runtime to repopulate its cache on demand.

References:

  • https://medium/deno-the-complete-reference/dependency-installation-in-deno-41caf01ec903
  • https://docs.deno/examples/node_built_in/
  • https://docs.deno/runtime/fundamentals/node/
Post a comment

comment list (0)

  1. No comments so far