Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

scala.js - node.js throwing "Buffer.alloc is not a function" error

I'm working on converting my ScalaJS 0.6.33 project to 1.x. I started with the transition to 1.0.0, as directed in the documentation. But I got errors that some of my dependencies were compiled with ScalaJS 1.3, so am now attempting to jump to 1.4.0.

My immediate problem is that running tests on my ScalaJS code throws an error originating with node.js:

sbt:xplatform> test
comSetup.js:7
  var inBuffer = Buffer.alloc(0);
                        ^

TypeError: Buffer.alloc is not a function
    at comSetup.js:7:25
    at comSetup.js:61:4
    at Object.exports.runInThisContext (vm.js:54:17)
    at [stdin]:8:27
    at Object.exports.runInThisContext (vm.js:54:17)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:425:26)
    at node.js:587:27
    at doNTCallback0 (node.js:428:9)
    at process._tickCallback (node.js:357:13)

This is after updating my installation of node to 15.7.0. I note that http://www.scala-js.org/doc/project/js-environments.html says nvm install 5.0. I tried that first (with the same result). I figured the 5.0 might be a typo, so tried the current version. Same result. I don't know what my previous version of node was.

Any suggestions on how to proceed? Selected lines (not in context!) from plugins.sbt that may be relevant:

addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.1.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.4.0")
addSbtPlugin("org.portable-scala"        % "sbt-scalajs-crossproject"  % "1.0.0")
addSbtPlugin("org.scala-js" % "sbt-jsdependencies" % "1.0.0")
libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"

and what I believe are the relevant parts of build.sbt:

lazy val commonSettings = Seq(
  scalaVersion := "2.12.13",
  ...
)

lazy val xplatform = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Full)
  .in(file("_xplatform"))
  .settings(commonSettings)

lazy val xplatformJVM = xplatform.jvm
lazy val xplatformJS  = xplatform.js

lazy val browser = (project
  .in(file("_browser")))
  .settings(commonSettings)
  .settings(
    scalaJSUseMainModuleInitializer := true,
    jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv(),
    libraryDependencies ++= Seq(
        "org.scala-js" %%% "scalajs-dom" % "1.1.0"  
      )
  )
  .enablePlugins(ScalaJSPlugin, ScalaJSWeb, JSDependenciesPlugin)
  .dependsOn(xplatformJS % "test->test;compile->compile")
question from:https://stackoverflow.com/questions/65927700/node-js-throwing-buffer-alloc-is-not-a-function-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@sjrd was correct, it was an old version of node.

I ran into one problem. In the course of trying to solve that, I read JavaScript Environments where it says to install nvm and then

nvm install 5.0

I did this without enough research. First, v5.0 appears to be very old. I wonder if this shouldn't be nvm install 15.0 (current as of this writing is 15.7).

I later installed v15 but v5.0 somehow became the default for new terminal sessions.

Once I got rid of v5.0, this particular problem went away.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...