Load GoJS as ES6 module in NodeJS

Hi,
When loading GoJS as an ES6 module in Node.js, I have the following message. It seems that ‘self’ keywords does not work in Dom-less environments ? Any suggestion ?

app.mjs
import * as go from ‘./es6/go-module.js’

node run command
node --experimental-modules app.mjs

error log
(node:15136) ExperimentalWarning: The ESM module loader is experimental.
file:///C:/Users/lm94893n/Git/projets/dem/src/node/es6/go-module.js:2149
export const go = self.go;

  •              ^*
    

ReferenceError: self is not defined

  • at file:///C:/Users/lm94893n/Git/projets/dem/src/node/es6/go-module.js:2149:19*
  • at ModuleJob.run (internal/modules/esm/module_job.js:109:37)*
  • at async Loader.import (internal/modules/esm/loader.js:132:24)*

What versions of Node and npm are you using?

I get the following error:

C:\temp\testm>node -v
v14.7.0

C:\temp\testm>npm -v
6.14.7

C:\temp\testm>more app.mjs
import * as go from "./go-module.js"

C:\temp\testm>node --experimental-modules app.mjs
C:\temp\testm\go-module.js:2149
export const go = self.go;
^^^^^^

SyntaxError: Unexpected token 'export'
←[90m    at wrapSafe (internal/modules/cjs/loader.js:1172:16)←[39m
←[90m    at Module._compile (internal/modules/cjs/loader.js:1220:27)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1277:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:1105:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:967:14)←[39m
←[90m    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:155:15)←[39m
←[90m    at ModuleJob.run (internal/modules/esm/module_job.js:140:23)←[39m
←[90m    at async Loader.import (internal/modules/esm/loader.js:165:24)←[39m
←[90m    at async Object.loadESM (internal/process/esm_loader.js:68:5)←[39m

Which is different from the error that you are getting.

EDIT: I can get your error if I rename go-module.js to go-module.mjs. But why didn’t I see your error?

EDIT2: Try changing the offending statement to be:
export const go = (typeof global !== “undefined”) ? global.go : self.go;

Here are the versions,

node -v
v12.15.0

npm -v
6.13.4

I dont know why there is a different error. Maybe newer node version enforces the .mjs extension for ES modules ?

I can get the module loading to work using the non-conditional ‘export const go = global.go;’

‘export const go = (typeof global !== “undefined”) ? global.go : self.go;’ gives the following error:

file:///C:/Users/lm94893n/Git/projets/dem/src/node/es6/go-module.js:2149
export const go = (global !== “undefined”) ? global.go : self.go;


SyntaxError: Invalid or unexpected token
    at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)

You forgot typeof

Thanks, It works with your solution. I had issues with the double-quotes