rollup plugin "@rollup/plugin-commonjs" parsing package.json and throwing unexpected token error

Viewed 517

TL;DR: Why is @rollup/plugin-commonjs looking in my package.json and how can I stop that so that I don't have to include @rollup/plugin-json arbitrarily to fix the problem?

I have a file written in JS for Node.js, CommonJS require() / module.exports() that I want to work in the Browser, so I am using Rollup to bundle and 'browserify' the Node.js code.

Environment

  • macOS Monterey Version 12.4
  • npm version 8.3.1
  • node version 16.14.0
  • rollup version 2.75.6
  • @rollup/plugin-json version 4.1.0

rollup.config.js

Here is my rollup.config.js:

// rollup.config.js
import commonjs from '@rollup/plugin-commonjs';
// import json from '@rollup/plugin-json';

const devMode = (process.env.NODE_ENV === 'development');
console.log(`${ devMode ? 'development' : 'production' } mode bundle`);

export default [
  {
    input: "test/_client_test.js",
    output: {
      file: 'build/test/test.js',
      format: 'iife'
    },
    plugins: [
      commonjs()
    ]
  }
];

Problem

When running npm rollup -c I get the following output:

[!] (plugin commonjs--resolver) Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
package.json (2:8)
1: {
2:   "name": "my-package",
           ^
3:   "version": "0.0.1",
4:   "description": "hello world",
Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
    at error (/***/node_modules/rollup/dist/shared/rollup.js:198:30)
    at Module.error (/***/node_modules/rollup/dist/shared/rollup.js:12553:16)
    at Module.tryParse (/***/node_modules/rollup/dist/shared/rollup.js:12930:25)
    at Module.setSource (/***/node_modules/rollup/dist/shared/rollup.js:12835:24)
    at ModuleLoader.addModuleSource (/***********/rollup.js:22309:20)
0 Answers
Related