Cole Mangio
Cole Mangio

Reputation: 79

Requiring normal javascript file from node compiled script?

Firstly, i want to state i'm very new to anything to do with node... Before i state my issue, here is some example code to refer to.

test.js

//test.js
const myMessage = 'Bananas';
export default myMessage; // Im not to sure about this line (problem)

main.js

//main.js
const test = require('./test.js');
console.log(test.myMessage);

I want to require a normal external javascript script called test.js from a node compiled script called main.js. I have compiled main.js simply by typing node main.js in my terminal. But node spat out an error 'Unexpected token export'. I know I'm doing something wrong here. Do i use "Modules"? How do i exclude the export statement?

Thanks for reading, sorry if my problem is making people facepalm on how dumb this issue might seem.

Upvotes: 0

Views: 931

Answers (3)

nanobar
nanobar

Reputation: 66365

Use babel register: https://babeljs.io/docs/en/babel-register

npm install @babel/core @babel/register @babel/preset-env --save

And require it in your main.js:

require('@babel/register')({
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: true
        }
      }
    ]
  ],
  ignore: [
    /node_modules/
  ]
});

This will parse other required files through babel which are not in node_modules, so ES6 import/export will work, and it will also polyfill features not present in your current version of node (node: true).

Note this should only be used if you have to require front-end scripts you can't reasonably modify. It's heavyweight to parse every require so if you do have to, make ignore as strict as possible (or even better use the only option) so you're only parsing what you need.

Upvotes: 1

Shubham Gupta
Shubham Gupta

Reputation: 2646

I think the external file you are trying to require is esModule. Such files can't be directly required unless you transpile them to commonJSModule. You have two solutions.

  • Transpile esModule to commonJSModule using babel. (or change export to module.exports in your test.js)
  • If you use new node version you can change it's extension to .mjs (rename test.js to test.mjs)

Please take a look at this Medium article which should help.

https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71

Upvotes: 4

Jonas Wilms
Jonas Wilms

Reputation: 138307

The export syntax is not yet supported in Nodejs (its in an alpha version), instead Nodejs provides a global object* (module.exports) which is what you get back with the require() call, so you just have to set that objects property to the thing you want to export:

 const myMessage = 'Bananas';
 module.exports.myMessage = myMessage;

or shorter:

 exports.myMessage = 'Bananas';

*global in the sense of "it exists although you haven't defined it", actually for every script that gets executed, a new module object will be created that can only be accessed inside of that script.

Upvotes: 1

Related Questions