Introduction

Modern JavaScript works best when using and authoring modules. The recommended way of using OpenLayers is installing the ol package. This tutorial walks you through setting up a simple dev environment, which requires node for everything to work.

In this tutorial, we will be using Parcel to bundle our application. There are several other options, some of which are linked from the README.

Initial steps

Create a new empty directory for your project and navigate to it by running mkdir new-project && cd new-project. Initialize your project using npm init and answer the questions asked.

Add OpenLayers as dependency to your application with

npm install ol

At this point you can ask NPM to add required development dependencies by running

npm install --save-dev parcel-bundler

Application code and index.html

Place your application code in index.js. Here is a simple starting point:

Creating a bundle

With simple scripts you can introduce the commands npm run build and npm start to manually build your bundle and watch for changes, respectively. Add the following to the script section in package.json: