# How to use .env files in Node.js with import syntax

> Learn how to use a .env file in a Node.js project with ES modules import syntax by installing the dotenv package and calling dotenv.config() in your script.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2023-01-11 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/how-to-use-env-files-in-nodejs-with-import-syntax/

I assume you have a [Node.js](https://flaviocopes.com/nodejs/) project set up to use ES modules, and you want to use a `.env` file to store a secret, like this:

```javascript
PASSWORD=secret
```

And you want to have it available in your Node.js script.

Here’s how to do it.

Install the `dotenv` package:

```javascript
npm i dotenv
```

Then use this code:

```javascript
import * as dotenv from 'dotenv'
dotenv.config()
console.log(process.env.PASSWORD)
```

This assumes you use ES modules (if not, it’s as easy as adding `"type": "module",` in your `package.json`)

By the way, if you need to turn a `.env` file into JSON or YAML (or the other way around), I built a free [env converter](https://flaviocopes.com/tools/env-converter/).
