1
votes

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?

+-- [email protected]
+-- [email protected]
+-- @rollup/[email protected]
+-- [email protected]
+-- @rollup/[email protected]
1
Can you share what do you have in your main.ts? - tmhao2005
Sure, I just imported a function from xml-js library import { js2xml } from 'xml-js' var xmlPoints = js2xml(points); - Zoner
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
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

1 Answers

2
votes

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.