package.json type module
See original GitHub issueDescribe the bug Using the new (but still exprimental) node esm loading mechanism does not work with the current version of ol’s package.json.
To Reproduce
npm i ol
- in index.js
import { createXYZ } from 'ol/tilegrid.js';
node --experimental-modules index.js
gives the following error:
Cannot use import statement outside a module
Expected behavior The module should be imported.
Fix
Add "type": "module"
to the package.json
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:22 (14 by maintainers)
Top Results From Across the Web
Modules: Packages | Node.js v19.3.0 Documentation
Within a package, the package.json "type" field defines how Node.js should interpret .js files. If a package.json file does ...
Read more >Why is 'type: module' in package.json file? - Stack Overflow
When you have "type": "module" in the package.json file, your source code should use import syntax. When you do not have, you should...
Read more >To load an ES module, set "type" - "module" in JavaScript
The "To load an ES module, set "type": "module" in package.json" error occurs when using the ES6 Modules syntax in a Node.js application...
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
Node.js supports a new setting in package.json called type . "type" can be set to either "module" or "commonjs" . ... This setting...
Read more >package.json - npm Docs
Don't use the same name as a core Node module. Don't put "js" or "node" in the name. It's assumed that it's js,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I decided to resurrect an old branch that converted our package to
"type": "module"
for the whole repo - not just for the published package. This wasn’t working that well with Node < 14 (when I last tried it) but is working better now.You can
npm install ol@module
to try it out. Here is a draft PR for theol-parcel
project: openlayers/ol-parcel#1.There is still work to do to convert the test utilities (and probably doc things). I’ll migrate #12241 out of draft state when that is done.
We have evaluated this and decided to wait with adding
"type": "module"
until ES modules are stable in Node (perhaps Node 14). In the meantime, you can use esm to make OpenLayers work in a Node environment.