Sbt / scalablytyped can't execute `npm` commands; no permission

[Update: see latest comment to see npm is identified as the source of the problem]

I’ve spent the last few afternoons when I have time trying to get ScalablyTyped setup on my scalajs project copied from sjrd’s vitejs template. I’ve looked at the docs, but there is always some mistake in the build.sbt, or someone suggests me to remove things like Compile in Compile / npmDependencies while the docs say to include it.

At this point my build.sbt appears to have no errors, but it now results in my entire codebase breaking as it uses scala 3’s whitespace indentation. Things like

class Foo:
  val s = 5

are now breaking because it expects { } instead of :

This is my Metals output

INFO  logging to files /Users/john/projects/game/.metals/metals.log
INFO  Started: Metals version 1.4.2 in folders '/Users/john/projects/game' for client Cursor 1.93.1.
Initializing Scala Debugger
...
WARN  no build target for: /Users/john/projects/game/src/Main.scala
INFO  no build target found for /Users/john/projects/game/src/Main.scala. Using presentation compiler with project's scala-library version: 2.12.19
INFO  no build target found for /Users/john/projects/game/src/Main.scala. Using presentation compiler with project's scala-library version: 2.12.19

I assume something in my build.sbt is incorrect and so rendering it unusable by Metals entirely, causing it to default to Scala 2.

this is my build.sbt

import org.scalajs.linker.interface.ModuleSplitStyle

lazy val game = project.in(file("."))
  .enablePlugins(ScalaJSPlugin)
  .enablePlugins(ScalablyTypedConverterPlugin)


  .settings(
    scalaVersion := "3.6.2",
    Compile / npmDependencies := Seq(
      "@types/three" -> "0.171.0"
    ),

    Compile / scalaSource := baseDirectory.value / "src/",

    scalacOptions ++= Seq(
      "-language:experimental.namedTuples"
    ),
    scalaJSUseMainModuleInitializer := true,

    scalaJSLinkerConfig ~= {
      _.withModuleKind(ModuleKind.CommonJSModule)
        .withModuleSplitStyle(
          ModuleSplitStyle.SmallModulesFor(List("game")))
    },
    libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.8.0",
)

this is my project/plugins.sbt

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scalablytyped.converter" % "sbt-converter" % "1.0.0-beta44")

I am helpless at this point. Can someone please tell me what simple thing I have wrong in the build.sbt?

I’ve done more investigation and found that this is caused by npm commands failing, likely due to lack of sudo privileges

[error] npm error A complete log of this run can be found in: /Users/john/.npm/_logs/2025-01-08T05_04_13_624Z-debug-0.log

said logs contain this


verbose stack FetchError: Invalid response body while trying to fetch https://registry.npmjs.org/randombytes: EACCES: permission denied, rename '/Users/john/.npm/_cacache/tmp/53e60154' -> '/Users/john/.npm/_cacache/content-v2/sha512/06/15/d1d06684507039b5fcb3f82c2c6eb8b85a5fb7692d04243b758ee9ba22e58004fb557e3879e7cc398abfc4fdd6dc4660be4995bbfc4d86464776dbb3f7eb'
2413 verbose stack     at /opt/homebrew/lib/node_modules/npm/node_modules/minipass-fetch/lib/body.js:170:15

I use a multi-user setup for homebrew. How can I get this working?

Fixed the npm errors. Now I am getting

INFO  Generated JavaScript file '/Users/john/projects/game/.bloop/game/game-test-js'
INFO  Scala test classes not supported by server
org.eclipse.lsp4j.jsonrpc.ResponseErrorException: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
    
Caused by: org.scalajs.testing.common.RPCCore$ClosedException: org.scalajs.testing.adapter.JSEnvRPC$RunTerminatedException

…What?? :pensive: Googling it yields zero results.