why import { version} from './package.json' in rollup.config file prompts version is not defined

1.2k Views Asked by At

I have these two ways to import version from my package.json file. And it prompts error says Error transforming bundle with 'rollup-plugin-license' plugin: version is not defined. Please see my following code.

import pkg from "./package.json";
import {version} from "./package.json";
import license from 'rollup-plugin-license';
export default {
  input: './src/a.js',
  output: {
    file: 'a.js',
    format: 'cjs',
  },
  plugins: [ 
    license({
      banner: `V<%= pkg.version %>`, //this works fine
      banner: `V<%= version %>`, //prompts version is not defined
    }),          
  ]
};
1

There are 1 best solutions below

0
On
banner: `V<%= pkg.version %>, //this works fine 

This statement works because you're importing your package.json into pkg with: import pkg from "./package.json"; and your package.json is a JSON object so you can use dot notation to reference properties of a JSON object. In this case, the version property of the package.json. However, this fails: banner: <V%= version %>, //prompts version is not defined because you haven't defined a version export in your package.json so when you use: import {version} from "./package.json"; version is undefined. See: https://medium.com/@trekinbami/a-not-so-in-depth-explanation-of-es6-modules-import-and-export-13a80300f2f0 for a quick explanation on ES6 importing/exporting modules.