JS TypeError on startup

When starting up the server I get the error below. It is rather a Node/Brunch problem than a Phoenix one, but I am a little bit confused because I can’t think of anything I changed with my general setup, especially because the error is thrown for every project on the machine.

  • nodejs v6.5.0
  • Phoenix v1.2.1

iex -S mix phoenix.server
Erlang/OTP 19 [erts-8.0.2] [source-753b9b9] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

[info] Running TestProject.Endpoint with Cowboy using http on port 4000
Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> /home/…/node_modules/anymatch/index.js:29
result = criterion.test(string) || altString && criterion.test(altString);
^

TypeError: Cannot assign to read only property ‘lastIndex’ of object ‘[object RegExp]’
at RegExp.exec (native)
at RegExp.test (native)
at testCriteria (/home/…/node_modules/anymatch/index.js:29:26)
at Array.some (native)
at anymatch (/home/…/node_modules/anymatch/index.js:59:48)
at FileList.isIgnored (/home/…/node_modules/brunch/lib/fs_utils/file_list.js:66:16)
at FileList._change (/home/…/node_modules/brunch/lib/fs_utils/file_list.js:185:26)
at emitMany (events.js:127:13)
at FileList.emit (events.js:201:7)
at BrunchWatcher.changeFileList (/home/…/node_modules/brunch/lib/watch.js:227:19)
at BrunchWatcher.startCompilation (/home/…/node_modules/brunch/lib/watch.js:292:12)
at FSWatcher.chokidar.watch.on.on.absPath (/home/…/node_modules/brunch/lib/watch.js:166:14)
at emitTwo (events.js:106:13)
at FSWatcher.emit (events.js:191:7)
at FSWatcher. (/home/…/node_modules/chokidar/index.js:171:15)
at FSWatcher._emit (/home/…/node_modules/chokidar/index.js:212:5)
at FSWatcher.NodeFsHandler._handleFile (/home/…/node_modules/chokidar/lib/nodefs-handler.js:275:10)
at FSWatcher. (/home/…/node_modules/chokidar/lib/nodefs-handler.js:473:21)
at FSReqWrap.oncomplete (fs.js:123:15)

It would have to be something in the “anymatch” library, may need to google for that. >.>

Yes, sure.

My question was rather if anybody could point me in a direction to why my general setup (no matter which project) suddenly throws the error on startup - because I did not change anything brunch/nodejs related.

I am running Ubuntu 14.04, Chokidar is a wrapper around fs for MacOS? When running npm update I get (as expected?):

├── babel-brunch@6.0.6 
└── phoenix@1.2.0  invalid

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.14

I am not sure why chokidar suddenly came into play and how to fix this (besides, of course, meddling with the node modules directly).

Some of the…interesting bit of the javascript ecosystem is that packages are tiny and change a lot (amazingly often from what I’ve seen). Probably something added in chokidar to add mac support and since you are not mac it will just do nothing, ignore its ‘WARN’. :slight_smile:

The ‘WARN’ is not an error, however you absolutely should not have an ‘invalid’ after Phoenix. Do you have a github example we can clone to try?

I’ve seen the same problem on Arch Linux and I have to wonder whether it was introduced during a recent node release (6.5.0; I’m using Phoenix 1.1.6). It seems that somewhere up stream some RegExp objects are being compromised as creating local copies seems to work around the issue (node_modules/anymatch/index.js):

case '[object RegExp]':

  // Workaround: create a fresh RegExp Object
  criterion  = new RegExp(criterion.source, criterion.flags);
  
  result = criterion.test(string) || altString && criterion.test(altString);
  break;
1 Like

In brunch do you have ‘compress’ set to true? If so set it to {} instead. I remember reading that somewhere…

Brunch 2.1.3 is the problem - it is possible that nodejs changed the way Object.freeze works. Updating to brunch 2.8.2 seems to fix that problem.

Basically brunch 2.1.3 has a deepFreeze function in application.js which indiscriminately freezes the entire configuration object - rendering the regular expression objects contained therein inoperable. The new version of brunch exercises specific exceptions to the freezing process - so the regular expressions remain operable. Node seems to have supported Object.freeze for a while - maybe it’s default behavior has changed in a more recent version.

2 Likes

Ah, cool, thanks for the followup!

I’m curious how you got brunch 2.1.3, that seems fairly ancient. I’m pretty sure I’ve not touched my package.json file yet I’m getting 2.8.2 in a fairly old phoenix app. Hmm…

As far as I recall, Brunch 2.1.3 was installed locally via Phoenix 1.1.6 which is the recommended package for working through Programming Phoenix. Everything worked fine back in June but as Arch is a rolling release npm/node has upgraded several times since then.

1 Like

Ahh, I initially started from the Phoenix 2.0 pre-release. That explains it. ^.^

I got rid the invalid error running npm update, but the initial error still persists. I am running brunch version 3.10.3.

The current version of brunch is 2.8.2 (since June 15, 2016). You’re initial error is due to operations failing on frozen RegExp objects (older versions of brunch mistakenly use Object.freeze on RegExp objects). I suspect that nodejs only recently implemented “full” freezing on RegExp objects (once they are immutable they are basically useless) or on objects inside of a function closure.

To fix your initial error go into the project’s package.json and change the brunch version to 2.8.2

"brunch": "~2.8.2",

Then run npm update (the newer version of brunch doesn’t freeze RegExp objects). Check the version that the local brunch installation reports before and after the update:

$ ./node_modules/brunch/bin/brunch -v
2.8.2
3 Likes

Thanks everyone. Updating package.json solved the problem.

3.10.3 was my npm version. :expressionless: