DataHub Docs
DataHubLearnCommunity
  • 👋Welcome to Figment Docs
  • 🚩Introduction
    • 🚀Why Build on Web 3?
    • 💻What is DataHub?
  • 📖DataHub Guides
    • 📍DataHub Products
      • Transaction Search API
      • Staking API
    • 🏭DataHub Infrastructure
    • 🏁Get Started With DataHub
    • 🚨Quotas & Rate Limits
    • 💡Dashboard how-tos
      • Configuring CORS whitelist
  • 📚Network Documentation
    • 🔭Network Guide
    • 🅰️Arbitrum
      • 🎮RPC & REST API
    • 🏔️Avalanche
      • 🎮RPC & REST API
        • Avalanche Indexer API
    • 💠Binance Smart Chain
      • 🎮RPC & REST API
    • 💰Celo
      • 💼CELO Wallet
      • 🎮RPC & REST API
    • ➰Centrifuge
      • 🎮RPC & REST API
    • 🌌Cosmos
      • 🎮RPC & REST API
      • 🎊Enriched APIs
        • Transaction Search
        • Rewards API
    • 💎Ethereum
      • 🎮RPC & REST API
    • 🧊Fantom
      • 🎮RPC & REST API
    • 🐦Kusama
      • 🎮RPC & REST API
    • 📱Mina
      • 🎮RPC & REST API
        • Indexer API Documentation
        • Query Mina GraphQL API
    • 🌈NEAR
      • 🎮RPC & REST API
      • 🎊Enriched APIs
        • Indexer API
      • 💼NEAR Wallet
    • 🏝️Oasis
      • 🎮RPC & REST API
        • Oasis REST API
    • 🧪Osmosis
      • 🎮RPC & REST API
    • 🍡Polkadot
      • 🎮RPC & REST API
      • 🎊Enriched APIs
        • Indexer API
        • Transaction Search
    • ⛽Polygon (Matic)
      • 🎮RPC & REST API
    • 🔋Solana
      • 🎮RPC & REST API
    • 📚Extra Guides
      • Blockchain Fundamentals
      • Docker Setup for Windows
      • Troubleshooting CORS Errors on DataHub
      • 5XX Retry Logic Best Practices
        • 5XX Retry Logic Best Practices - NodeJS
        • 5XX Retry Logic Best Practices - Python
        • 5XX Retry Logic Best Practices - Ruby
        • 5XX Retry Logic Best Practices - Go
      • Setting up a fresh JavaScript Project with dotenv
      • Getting started with dotenv and .env files
      • Rust Learning Resources
      • Setup Solana BPF Toolchain on Windows
      • Figment Learn Pathway Troubleshooting
  • 🤔Other
    • 🧾Glossary
    • 🗳️Support
  • 🔗Terms & Conditions
    • Terms of Use
    • Terms & Conditions DataHub
    • Privacy Policy
    • Contributor Terms
Powered by GitBook
On this page
  • Make sure Node.js is installed
  • Initialize the project directory
  • References

Was this helpful?

  1. Network Documentation
  2. Extra Guides

Setting up a fresh JavaScript Project with dotenv

This guide explains how to set up a new project directory for working with example JavaScript code

Previous5XX Retry Logic Best Practices - GoNextGetting started with dotenv and .env files

Last updated 3 years ago

Was this helpful?

Make sure Node.js is installed

If you are installing Node.js on WSL, you can .

Once the installation is complete, or if you already have Node.js installed, type the command node -v inside a terminal window (such as bash, zsh, cmd.exe or PowerShell) to check the currently installed version.

Initialize the project directory

mkdir samplecode
cd samplecode
npm init -y
npm install --save dotenv

These commands accomplish the following :

  • mkdir creates our project directory, the directory name can be changed to anything suitable - it does not need to remain samplecode.

  • The cd command changes into the newly created directory (making it the working directory).

  • Run the node package manager - npm , optionally with the -y flag to skip the prompts and generate a default package.json .

+-- /samplecode
    |
    +-- package.json
    |
    +-- package-lock.json
    |
    +-- /node_modules
        |  
        +-- /dotenv

Since we used the --save flag when installing, it will be much easier to see which dependencies are installed by looking inside the package.json :

{
  "name": "samplecode",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^9.0.2"
  }
}
"type": "module",
  • If we are choosing to use the older, slightly more common require() syntax, then it is unnecessary to add this line to package.json .

With these steps completed, we now have a project directory that is prepared to have other dependencies installed into it using npm install or yarn add .

This basic setup is a sufficient start to be able to write and execute further JavaScript code using Node.js, the next step being to install whichever dependencies we would like to learn about. This is a good way to experiment and become familiar with particular JavaScript libraries.

References

Official releases of Node.js can be .

Most modern JavaScript APIs will target Node.js v12+. Using a version manager such as or is strongly encouraged, as it allows developers to quickly switch between Node.js versions as needed.

Install the package, which will allow us to access environmental variables in our code. Assuming no errors during the installation of dotenv, the project directory will now contain these files and directories in the following structure :

For anybody unfamiliar with environment variables, refer to our guide on at this point to understand the package and what it is used for.

If we are choosing to use the more modern , when developing we will need to add a line to our package.json in order to prevent aSyntaxError: Cannot use import statement outside a module . The import keyword was introduced in Node.js v12, a language feature to simplify the use of modules. Alternatively, we could use an .mjs file extension for all of our JavaScript files, however adding this line to package.json enables us to keep the .js file extension and saves a lot of hassle with configuration :

📚
📚
downloaded from the official site
nvm
fnm
dotenv
dotenv and .env
ES6 import syntax
follow this guide
So fresh and so clean!
ES6 Modules and How to Use Import and Export in JavaScript | DigitalOceanDigitalOcean
Logo