deno icon indicating copy to clipboard operation
deno copied to clipboard

Always create a `deno.json` file when running `deno init --npm ...`

Open mintydev789 opened this issue 6 months ago • 6 comments

Right now, if you initialize most framework projects with Deno, a deno.lock file is created, but not a deno.json file. I think it would be best to have Deno automatically create a deno.json file and place the NPM scripts in there as Deno tasks, only leaving the package.json file to contain the NPM dependencies. Perhaps, it could also have "nodeModulesDir": "auto" for projects that depend on NPM dependencies.

This would make it clearer that the user is working with a Deno project and that they should use deno.json for configuration. From personal experience, it feels odd initializing a project with Deno and still feeling like I'm working with Node and NPM.

Perhaps, it could also somehow replace any eslintrc.cjs files with a lint configuration in the deno.json file, although I can see that being more difficult to achieve. Same for .prettierrc -> fmt configuration and tsconfig.json -> compilerOptions configuration. If this is something the Deno team wants to do, I'd be happy to open another issue, but the simple deno.json setup outlined above is something I'd really like to see added, as someone who has used Deno with a number of frameworks by now.

mintydev789 avatar May 29 '25 11:05 mintydev789

I'm not sure I understand - you can use deno init to initialize a Deno project.

bartlomieju avatar May 29 '25 11:05 bartlomieju

I'm not sure I understand - you can use deno init to initialize a Deno project.

I know, that's not what this is about. The description says deno init should always set up a deno.json file with at least a minimal configuration with the tasks and nodeModulesDir. This only applies to projects that rely on NPM packages, of course (e.g. Next JS, Solid Start, Astro, etc.).

mintydev789 avatar May 29 '25 12:05 mintydev789

@mintydev789 I don't understand either. Can you outline the initial state of the repo, the steps the user takes, and then the desired end state of the repo?

dsherret avatar May 29 '25 15:05 dsherret

@mintydev789 I don't understand either. Can you outline the initial state of the repo, the steps the user takes, and then the desired end state of the repo?

Sure. So let's take Astro as an example. First, create an Astro starter project using the command from the Deno docs: deno init --npm astro@latest. Use the minimal (empty) template and agree to install dependencies, then cd into the created project directory. These are now the contents of the created project we should see:

astro.config.mjs  deno.lock  node_modules  package.json  public  README.md  src  tsconfig.json

There is a deno.lock file, which makes sense, a tsconfig.json file, which is fine, since Astro relies on Typescript, and a package.json file, which is also fine, since all dependencies are NPM packages. However, there is no deno.json file, which we should expect from a project created using Deno.

My proposal is that when we initialize NPM-based projects using Deno, we still create a deno.jsonc file (.jsonc is better, since it allows comments), we put "nodeModulesDir": "auto" in there, and we move the scripts from package.json there (so there'd be no "scripts" block in there at all). So with our Astro example, we end up with the following deno.jsonc file:

{
  "nodeModulesDir": "auto",

  "scripts": {
    "dev": "astro dev",
    "build": "astro build",
    "preview": "astro preview",
    "astro": "astro"
  }
}

I believe that this would help in "integrating" developers adopting Deno into the ecosystem. It will then be clear to them that they can configure Deno to their liking using the deno.jsonc file. Just imagine if Typescript just threw you into the deep end with no tsconfig.json file.

One thing to consider is that some project setup scripts directly tell you to run an NPM command after installation. So I think perhaps this could be added as an optional feature, with deno init prompting you at the end of the command if you want to create a deno.jsonc file, and if the user accepts displaying a message like "Note: NPM scripts will be moved to deno.jsonc, so you will have to run deno task [script] instead of npm run [script]", or something among those lines. The specifics can be discussed further, of course.

mintydev789 avatar May 29 '25 16:05 mintydev789

I also just noticed another issue: when you use deno init --npm, and the project setup requires some NPM package, it creates a node_modules directory. It would be nice if deno init stored the dependencies it requires somewhere else temporarily. Example: deno init --npm solid creates node_modules/create-solid, which is useless after installation. Should this be a separate issue?

mintydev789 avatar May 29 '25 16:05 mintydev789

Should this be a separate issue?

Yes.

dsherret avatar May 29 '25 18:05 dsherret