ReferenceError: Cannot access 'ShareJS' before initialization

908 views Asked by At

I was building a collaborative text editor web application using meteor. For the same I added a package named "mizzao:sharejs-codemirror" and after that my application crashed on starting and displayed following error

ReferenceError: Cannot access 'ShareJS' before initialization
at module (packages/mizzao:sharejs/sharejs-server.js:8:24)
at fileEvaluate (packages\modules-runtime.js:336:7)
at Module.require (packages\modules-runtime.js:238:14)
at require (packages\modules-runtime.js:258:21)
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\packages\mizzao_sharejs.js:130:15
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\packages\mizzao_sharejs.js:135:3
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\boot.js:401:38
at Array.forEach (<anonymous>)
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\boot.js:226:21
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\boot.js:464:7
at Function.run (D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\profile.js:280:14)
at D:\Workspace\web\course4\week (1)\textcircle\.meteor\local\build\programs\server\boot.js:463:13

Can anyone tell me where i might be going wrong thank you.

1

There are 1 answers

0
coagmano On

Looks like that comes from this line:

export const ShareJS = ShareJS || {};

Which, as the error says, tries to assign the variable ShareJS to the contents of ShareJS, which isn't defined until after the expression is finalized.

This would have worked in older Meteor versions because to support older browsers const declarations were transpiled to var (with some helpers to prevent re-assignment). Recently, Meteor has started producing modern bundles for modern browsers, which leave code like const untouched.

Unfortunately this code was incorrect from the start, but didn't show up as buggy till now.

The easiest way around it is to edit the line yourself. You can make a local fork by downloading the source from github into the packages folder of your app. Then change the const to var.

You will need to edit the sharejs-base package.

Alternatively, you can use an up to date version of ShareJS from npm, and implement the server and client portion yourself.