1

I want to use xml-js library with rollup bundler. After simple import I got an error, that 'json2xml' is not exported by node_modules\xml-js\lib\index.js

I tried to fix it with latest version of commonjs rollup plugin. After trying to build, I got this exception

[!] TypeError: Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at new MagicString (C:\Users\x\AppData\Roaming\npm\node_modules\rollup\dist\shared\rollup.js:580:34)
    at Module.setSource (C:\Users\x\AppData\Roaming\npm\node_modules\rollup\dist\shared\rollup.js:10038:28)
    at ModuleLoader.addModuleSource (C:\Users\maxim\AppData\Roaming\npm\node_modules\rollup\dist\shared\rollup.js:18158:20)

rollup.config.js looks very simple

import typescript from 'rollup-plugin-typescript2';
import json from '@rollup/plugin-json';
import nodeResolve from '@rollup/plugin-node-resolve';
import common from "@rollup/plugin-commonjs"

export default {
    input: ['./main.ts'],
    plugins: [
        nodeResolve(),
        common(),
        typescript({}),
        json(),
    ],
    output: {
        dir: 'output',
        format: 'iife'
    },
}

After intalling same rollup plugins in empty project, I got same error. Do you have any suggestions?

+-- xml2js@0.4.23
+-- rollup@2.26.10
+-- @rollup/plugin-json@4.1.0
+-- rollup-plugin-typescript2@0.29.0
+-- @rollup/plugin-commonjs@16.0.0
Zoner
  • 73
  • 8
  • Can you share what do you have in your `main.ts`? – tmhao2005 Nov 09 '20 at 17:24
  • Sure, I just imported a function from xml-js library `import { js2xml } from 'xml-js' var xmlPoints = js2xml(points);` – Zoner Nov 10 '20 at 09:35
  • I don't see anything wrong with your provide code except for the package `xml2js` not reflecting the thing you imported `xml-js` but it can't be an issue anyway. Do you have a reproducible repo? (One thing I know that above code is likely from `commonjs` plugin was trying to convert the `esm` module which is supposed to be `cjs` instead) – tmhao2005 Nov 10 '20 at 10:28
  • I was actually able to build an output on other machine. So, I may be a version issue of some packages I have installed globally? – Zoner Nov 11 '20 at 17:20

1 Answers1

2

I fixed the issue by removing global npm packages (C:\Users\x\AppData\Roaming\npm and C:\Users\x\AppData\Roaming\npm-cache) aswell as local node_modules and installing latest versions of all needed packages from scratch.

Zoner
  • 73
  • 8
  • Thank you! Had the same error and resolved it the some way -- by uninstalling rollup `npm uninstall --global rollup` then installing it again `npm install --global rollup`. – Jay Alammar Nov 16 '20 at 06:34