Search code examples
typescriptversioning

How to get the version from the package.json in Typescript?


I have found this Q&A.

I have tried to get the version from the package.json using:

import { version } from './package.json';

but it results in:

package.json' has unsupported extension. The only supported extensions are '.ts',
'.tsx', '.d.ts'.

Solution

  • If you are are in a commonJS environment, why not simply use

    const pj = require('./package.json')
    console.log(pj.version)
    

    EDIT

    Since you seem to be using webpack, just add the appropriate loader

       module: {
        loaders: [
            ...
            {test: /\.json$/, loader: 'json-loader'},
            ....
    

    which obviously you need to install using npm install