![Yoctopuce Yocto-GPS User Manual Download Page 45](http://html1.mh-extra.com/html/yoctopuce/yocto-gps/yocto-gps_user-manual_3415557045.webp)
To give it a try, go into one of the example directory (for instance example_nodejs/Doc-Inventory).
You will see that it include an application description file (package.json) and a source file (demo.js).
To download and setup the libraries needed by this example, just run:
npm install
Once done, you can start the example file using:
node demo.js
Using a local copy of the Yoctopuce library with node.js
If for some reason you need to make changes to the Yoctopuce library, you can easily configure your
project to use the local copy in the
lib/
subdirectory rather than the official npm package. In order
to do so, simply type the following command in your project directory:
npm link ../../lib
Using the Yoctopuce library within a browser (HTML)
For HTML examples, it is even simpler: there is nothing to install. Each example is a single HTML file
that you can open in a browser to try it. In this context, loading the Yoctopuce library is no different
from any standard HTML script include tag.
Using the Yoctoluce library on older JavaScript engines
If you need to run this library on older JavaScript engines, you can use Babel
3
to transpile your code
and the library into older JavaScript standards. To install Babel with typical settings, simply use:
npm instal -g babel-cli
npm instal babel-preset-env
You would typically ask Babel to put the transpiled files in another directory, named
compat
for
instance. Your files and all files of the Yoctopuce library should be transpiled, as follow:
babel --presets env demo.js --out-dir compat/
babel --presets env ../../lib --out-dir compat/
Although this approach is based on node.js toolchain, it actually works as well for transpiling
JavaScript files for use in a browser. The only thing that you cannot do so easily is transpiling
JavaScript code embedded directly in an HTML page. You have to use an external script file for
using EcmaScript 2017 syntax with Babel.
Babel has many smart features, such as a watch mode that will automatically refresh transpiled files
whenever the source file is changed, but this is beyond the scope of this note. You will find more in
Babel documentation.
Backward-compatibility with the old JavaScript library
This new library is not fully backward-compatible with the old JavaScript library, because there is no
way to transparently map the old blocking API to the new asynchronous API. The method names
however are the same, and old synchronous code can easily be made asynchronous just by adding
the proper
await
keywords before the method calls. For instance, simply replace:
beaconState = module.
get_beacon
()
;
by
3
http://babeljs.io
7. Using Yocto-GPS with JavaScript / EcmaScript
www.yoctopuce.com
39
Summary of Contents for Yocto-GPS
Page 1: ...Yocto GPS User s guide...
Page 2: ......
Page 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Page 10: ...4 www yoctopuce com...
Page 14: ...8 www yoctopuce com...
Page 18: ...12 www yoctopuce com...
Page 22: ...16 www yoctopuce com...
Page 38: ...32 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 54: ...48 www yoctopuce com...
Page 72: ...66 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 122: ...116 www yoctopuce com...
Page 132: ...126 www yoctopuce com...
Page 564: ...22 Characteristics 558 www yoctopuce com...
Page 565: ......
Page 566: ......