# How to Read Package Version in node.js Code?

To read the package version in Node.js code, you can use the `require` function to import the `package.json` file and access the `version` property. Here's an example:

Assuming your project structure looks like this:

```
project-root
│   package.json
│   yourScript.js
```

1. Modify your `package.json` file to include a `version` field:
    
    ```json
    {
      "name": "your-project",
      "version": "1.0.0",
      "description": "Your project description",
      // other fields...
    }
    ```
    
2. In your Node.js script (`yourScript.js`), read the package version:
    
    ```jsx
    const packageInfo = require('./package.json');
    
    // Access the version property
    const version = packageInfo.version;
    
    console.log('Package version:', version);
    ```
    
    Make sure to adjust the path in the `require` statement if your script is in a different location relative to the `package.json` file.
    

Alternatively, you can use the `fs` (file system) module to read the contents of the `package.json` file and then parse the JSON data to retrieve the version:

```jsx
const fs = require('fs');

// Read the content of package.json
const packageJsonContent = fs.readFileSync('./package.json', 'utf8');

// Parse the JSON data
const packageInfo = JSON.parse(packageJsonContent);

// Access the version property
const version = packageInfo.version;

console.log('Package version:', version);
```

Using `require` is a more convenient and common approach, but reading the file manually gives you more flexibility in case you need to perform additional operations on the `package.json` data.