2

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
    }),          
  ]
};
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Blake
  • 7,367
  • 19
  • 54
  • 80

1 Answers1

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

Nathan
  • 7,853
  • 4
  • 27
  • 50