In a Java application, a new context can be created using the
New contexts can be configured in multiple ways, and configuration options include exposing access to Java classes, allowing access to IO, etc.
A list of context creation options can be found in the API documentation.
In a Node.js application, the GraalVM
Context executing the application is pre-initialized by the Node.js runtime, and cannot be configured by the user application.
In this scenario, Java classes can be exposed to the Node.js application by using the
--vm.cp= command line option of the
bin/node command, as described below.
Java built-in object.
The object is not available by default, and can be enabled in the following way:
- In Node.js, start GraalVM using the
- In Java, create a GraalVM context using the
- In Node.js, multiple contexts can be created using Node.js' Worker threads API. The worker threads API ensures that no sharing can happen between two parallel contexts.
- In Java, multiple contexts can be executed from multiple threads. As long as a context is not accessed by two threads at the same time, parallel execution happens safely.
Java built-in object.
In order for a Java library to be accessible from a
jar files need to be added to the GraalVM class path.
This can be done in the following way:
- In Node.js, the classpath can be modified using the
- In Java, the default Java's
-cpoption can be used.
More details on GraalVM command line options are available in docs/user/Options.md.
npm package registry can be used from Node.js as well as from Java.
CommonJS (CJS) modules are supported when running with Node.js.
CommonJS modules cannot be used directly from Java; to this end, any popular package bundlers (such as Parcel, Browserify or Webpack) can be used.
ECMAScript Modules (ECM)
- The support of ECMAScript modules in Node.js is still experimental.
GraalVM supports all features supported by the Node.js version that is compatible with GraalVM.
To check such version, simply run
- ECMAScript modules can be loaded in a
More details about evaluating files using the Context API are available in the API Javadoc.
CommonJS Modules (CJS)
- In Node.js, modules can be loaded using the
require()built-in function, as expected.
ContextAPI does not support CommonJS modules, and has no built-in
require()function. In order to be loaded and used from a