SaxonJS in NodeJS
Asked Answered
K

2

0

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?

Katlynkatmai answered 14/9, 2020 at 17:5 Comment(0)
J
1

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

Joinery answered 23/2, 2021 at 22:17 Comment(0)
S
0

Try

require('SaxonJS2N.js')

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

Sonja answered 14/9, 2020 at 19:48 Comment(2)
Thank for the answer, but it didn't make any difference. In package.json of saxon-js, it seems that main file being imported is actually SaxonJSN.js. Could you be kind to explain how SaxonJSN.js determine whether it is running from browser or node.js? I'm running the code inside jest testing framework to transform xml and validate them with expected output. Perhaps it's something I need to configure from testing framework.Katlynkatmai
This is turning into more of a support ticket than a simple question-and-answer, so I suggest you raise it as a Saxon-JS enquiry on saxonica.plan.io. We haven't done any testing under jest so this will need some investigation. It would be a good idea if you raise a ticket to tell us exactly what we need to do to reproduce the problem.Sonja

© 2022 - 2024 — McMap. All rights reserved.