How to get version from package.json in Node.js code?

Sometimes, we want to get version from package.json in Node.js code.

In this article, we’ll look at how to get version from package.json in Node.js code.

How to get version from package.json in Node.js code?

To get version from package.json in Node.js code, we can import the content of package.json.

For instance, we write

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

to import the version property from package.json as a string.

Conclusion

To get version from package.json in Node.js code, we can import the content of package.json.