SaxonJS in NodeJS

872 views Asked by At

I'm having trouble importing SaxonJS to NodeJS application.

const saxonJs = require("saxon-js");

console.log(Object.getOwnPropertyNames(saxonJs));
// [ ]

console.log(Object.getOwnPropertyNames(window.SaxonJS));

/**
[
      'atom',              'Atomic',
      'transform',         'Serializer',
      'serialize',         'StringWriter',
      'getProcessorInfo',  'getResource',
      'setLogLevel',       'getLogLevel',
      'setPlatform',       'getPlatform',
      'getNavigator',      'timeStamp',
      'internalTransform', 'checkOptions',
      'makeAtomicValue',   'getItemDetails',
      'XdmArray',          'XdmFunction',
      'XdmMap',            'U',
      'XError',            'XPath',
      'XS',                'Developer'
    ]
    */

const testOutput = saxonJs.transform({
                    stylesheetFileName: xslt_filepath,
                    sourceFileName: source_filename,
                    destination: 'raw',
                });
// returns TypeError: saxonJs.transform is not a function

window.SaxonJS.transform({
                    stylesheetFileName: xslt_filepath,
                    sourceFileName: inputFiles[j],
                    destination: 'raw',
                });
// XError: Cannot supply stylesheetFileName in browser

returns TypeError: saxonJs.transform is not a function

what is going on?

2

There are 2 answers

2
Michael Kay On

Try

require('SaxonJS2N.js')

I think you've picked up the browser version of Saxon-JS rather than the Node.js version.

0
Josh Begin On

I ran into this same error and was able to resolve it by adding the below to my package.json file. This makes Jest use node environment instead of the jsdom (default):

"jest": { "testEnvironment": "node" }

https://jestjs.io/docs/en/configuration#testenvironment-string