I just started working with Node JS and the ESM Standard with the import statement. I got the repeated error SyntaxError: Cannot use import statement outside a module. I searched the internet and this is the closest I came with finding information regarding modules.
Put the following in b.js
. It has the value we want to export
<code>export const myValue = 42;</code>
Put this in a.js
. It will import the value from b.js
<code>import { myValue } from './b.js'; console.log('The value of b is:', myValue); // use imports</code>
To make all this work, we need to create a package.json
file, so that it will allow the import
.
<code>{ "name": "my-package", "version": "1.0.0", "type": "module" }</code>
Now run it.
<code>$ node a.js</code>
Output should look like this
<code>The value of b is: 42</code>