commonjsrollupmagic-string

Rollup commonJs and nodeResolve MagicString error


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

Solution

  • 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.