Import from/Export to JS
Note: due to JS ecosystem's module compatibility issues, our advice of keeping your ReScript file's compiled JS output open in a tab applies here more than ever, as you don't want to subtly output the wrong JS module import/export code, on top of having to deal with Babel/Webpack/Jest/Node's CommonJS<->ES6 compatibility shims.
In short: make sure your bindings below output what you'd have manually written in JS.
module.export = ....
import * from 'MyReScriptFile'and
export let ....
The format is configurable in via
Here's what the
@module("path"): pass the name of the JS module; in this case,
"path". The string can be anything:
external: the general keyword for declaring a value that exists on the JS side.
dirname: the binding name you'll use on the ReScript side.
string => string: the type signature of
dirname. Mandatory for
= "dirname": the name of the variable inside the
pathJS module. There's repetition in writing the first and second
dirname, because sometime the binding name you want to use on the ReScript side is different than the variable name the JS module exported.
By omitting the string argument to
module, you bind to the whole JS module:
Depending on whether you're compiling ReScript to CommonJS or ES6 module, this feature will generate subtly different code. Please check both output tabs to see the difference. The ES6 output here would be wrong!
Use the value
"default" on the right hand side:
As mentioned in ReScript's idiomatic Import & Export, every let binding and module is exported by default to other ReScript modules (unless you use a
If your JS project uses ES6 modules, you're likely exporting & importing some default values:
// student.js export default name = "Al";
// teacher.js import studentName from 'student.js';
default (now you know!). So to export a default value from ReScript, you can just do:
You can then import this default export as usual on the JS side:
// teacher2.js import studentName from 'ReScriptStudent.js';
requires, we've taken care of that too! See the CommonJS output tab for