Running Aurelia in Node.js

I need to run my Aurelia application in Node.js. Acutally, only part of it: I don’t need any of the UI stuff, just the dependency injection and the store plugin which are used in an area of the app that’s concerned only with business logic. Still those need to be set up somehow and I thought it might be easiest to use SSR, which should handle running the application in Node. So I set up a new Aurelia project, installed the dependencies …

au new ssr
npm install
npm install --save aurelia-ssr-bootstrapper-webpack
npm install --save aurelia-pal-nodejs

and copied over the code from the sekeleton. However, I’m getting build errors like this:

ERROR in ./node_modules/canvas/build/Release/canvas.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See
(Source code omitted for this binary file)
 @ ./node_modules/canvas/lib/bindings.js 3:17-56
 @ ./node_modules/canvas/index.js
 @ ./node_modules/jsdom/lib/jsdom/utils.js
 @ ./node_modules/jsdom/lib/jsdom/browser/Window.js
 @ ./node_modules/jsdom/lib/api.js
 @ ./node_modules/aurelia-pal-nodejs/dist/nodejs-pal-builder.js
 @ ./node_modules/aurelia-pal-nodejs/dist/index.js
 @ ./node_modules/aurelia-ssr-bootstrapper-webpack/dist/native-modules/aurelia-ssr-bootstrapper-webpack.js
 @ ./src/ssr.ts
 @ multi aurelia-webpack-plugin/runtime/empty-entry aurelia-webpack-plugin/runtime/pal-loader-entry ssr

Or when trying to use it in the actual project:

ERROR in ./node_modules/iconv-lite/encodings/tables/gb18030-ranges.json
Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = {"u...'
File was processed with these loaders:
 * ./node_modules/json-loader/index.js
You may need an additional loader to handle the result of these loaders.
SyntaxError: Unexpected token m in JSON at position 0 while parsing near 'module.exports = {"u...'
    at JSON.parse (<anonymous>)
    at parseJson (.../node_modules/json-parse-better-errors/index.js:7:17)
    at JsonParser.parse (.../node_modules/webpack/lib/JsonParser.js:16:16)
    at .../node_modules/webpack/lib/NormalModule.js:482:32
    at .../node_modules/webpack/lib/NormalModule.js:358:12
    at .../node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (.../node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at iterateNormalLoaders (.../node_modules/loader-runner/lib/LoaderRunner.js:221:10)
    at runSyncOrAsync (.../node_modules/loader-runner/lib/LoaderRunner.js:130:11)
    at iterateNormalLoaders (.../node_modules/loader-runner/lib/LoaderRunner.js:232:2)
    at Array.<anonymous> (.../node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (.../node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
    at .../node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
    at .../node_modules/graceful-fs/graceful-fs.js:123:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)
 @ ./node_modules/iconv-lite/encodings/dbcs-data.js 106:37-76
 @ ./node_modules/iconv-lite/encodings/index.js
 @ ./node_modules/iconv-lite/lib/index.js
 @ ./node_modules/whatwg-encoding/lib/whatwg-encoding.js
 @ ./node_modules/jsdom/lib/api.js
 @ ./node_modules/aurelia-pal-nodejs/dist/nodejs-pal-builder.js
 @ ./node_modules/aurelia-pal-nodejs/dist/index.js
 @ ./src/ssr.ts
 @ multi aurelia-webpack-plugin/runtime/pal-loader-entry ssr

So both issues are loader related, somehow not the correct loader is used. I assume, this is a version mismatch between Aurelia, webpack, and the involved plugins. Does anybody know how to get Aurelia running in Node.js with up to date versions?

Or should I ditch SSR altogether and try to implement a minimal runner that initializes the Container and Store plugin “manually”?

1 Like

The later would be preferable.
You might even go for Au2 DI from kernel and store plugin which was just released.
Although that’s kind of uncharted territory.

Also running under node and webpack in the same sentence rings wrong bells…