require('babel/register') doesn't work

requires synonym
require meaning
required
requirements
require js
required meaning
how to pronounce require.
require in a sentence

I have isomorphic app written in ES6 on client with Babel transpiler. I want my express server to have the same ES6 syntax as client code.

Unfortunately require('babel/register') doesn't work..

server.js
require('babel/register'); // doesn't work
// require('babel-core/register); doesn't work..

const env = process.env.NODE_ENV || 'development';
const port = process.env.NODE_PORT || 1995;

const http = require('http');
const express = require('express');
const address = require('network-address');

let app = express();

app.set('port', port);
app.use(express.static(path.join(__dirname, 'public')));

app.get('*', (req, res) => {
   res.send('Hello!');
});

http.createServer(app).listen(app.get('port'), function () {
   console.info('Demo app is listening on "%s:%s" env="%s"', address(), app.get('port'), env);
});

require('babel/register') doesn't transpile the file it is called from. If you want server.js to be included in on-the-fly transpilation, you should execute it with babel-node (Babel's CLI replacement for node).

See my answer here for an example.

Require Synonyms, Require Antonyms, SWAP require IN A SENTENCE. Join our early testers! See how your sentence looks with different synonyms. TRY NOW. Characters  Require definition, to have need of; need: He requires medical care. See more.

Since Babel 6 use babel-register hook to make on-the-fly transpilation.

First:

 npm install babel-register

Then require it with:

require('babel-register');    
// not using 
// require('babel/register');
// or 
// require('babel-core/register);

To Convert your Ecmascript 6 code to ecmascript 5, you must set Babel presets option with require babel-register Like this:

require('babel-register')({
  presets: [ 'es2015' ]
});

Unlike the answer of @alexander-pustovalov you do not need to .babelrc file.

you must also install babel-preset-es2015:

npm install babel-preset-es2015

Finally your Server.js file will be:

require('babel-register')({
   presets: [ 'es2015' ]
});

const env = process.env.NODE_ENV || 'development';
const port = process.env.NODE_PORT || 1995;

const http = require('http');
const express = require('express');
const address = require('network-address');

let app = express();

app.set('port', port);
app.use(express.static(path.join(__dirname, 'public')));

app.get('*', (req, res) => {
   res.send('Hello!');
});

http.createServer(app).listen(app.get('port'), function () {
   console.info('Demo app is listening on "%s:%s" env="%s"', address(), app.get('port'), env);
});

Require, transitive verb. 1a : to claim or ask for by right and authority. b archaic : request. 2a : to call for as suitable or appropriate the occasion requires formal dress. b : to demand as necessary or essential : have a compelling need for all living beings require food. 1. need, crave, depend upon, have need of, want, miss, lack, wish, desire, stand in need of A baby requires warmth and physical security. 2. demand, take, involve, call for, entail, necessitate This requires thought, effort, and a certain ruthlessness.

I ran into a similar issue trying to render a react page (.jsx) on the server. I fixed it by putting the snippet below at the top of my server file

require('babel-register')({
    presets: ['es2015', 'react']
});

make sure you have npm babel-preset-es2015 and babel-preset-react installed

Require Synonyms, Require Antonyms, Some common synonyms of require are claim, demand, and exact. While all these words mean "to ask or call for something as due or as  "reQuire is the ability to close with peace in mind. The importance to know that your closings are professionally handled and followed up on allows me to do what I do best while providing customer service beyond expectation. I love the tracking features and we always get the best phone support. I wish we had this 20 years ago!"

According to this document you have to use:

require("babel-register");

Additionally, you have to put .babelrc file in the root of directory from which you start server.

{
  "presets": ["es2015"]   
}

Require, Require definition, to have need of; need: He requires medical care. See more. require definition: 1. to need something or make something necessary: 2. to need something or make something…. Learn more.

In the eve of 2019 we still have no good documentation in JS-related libraries, but, on the other hand, we have StackOverflow for that.

In order to use babel on Node.js, you need to

  1. npm install @babel/register @babel/core @babel/preset-env
  2. Create a file pre-index.js with attached contents
  3. Run node pre-index

You can use imports and other features only in index.js and files it imports or requires.

require('@babel/register')({
    presets: [
        [
            "@babel/preset-env",
            {
                targets: {
                    node: "current"
                }
            }
        ]
    ]
});
require('./index.js');

require, require meaning, definition, what is require: to need something: Learn more. Word forms: 3rd person singular present tense requires, present participle requiring, past tense, past participle required 1. verb If you require something or if something is required, you need it or it is necessary.

REQUIRE (verb) definition and synonyms, What is the definition of require? What is the meaning of require? How do you use require in a sentence? What are synonyms for require? Some common synonyms of require are claim, demand, and exact. While all these words mean "to ask or call for something as due or as necessary," require suggests the imperativeness that arises from inner necessity, compulsion of law or regulation, or the exigencies of the situation. the patient requires constant attention

Require, Require definition: If you require something or if something is required , you need it or it is necessary. | Meaning, pronunciation, translations and examples. Synonyms for require at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for require.

Require definition and meaning, The best 44 synonyms for require, including: want, demand, request, lack, feel the necessity for, have need for, oblige, necessitate, claim, exact, requisition and​  Required definition, to have need of; need: He requires medical care. See more.

Comments
  • What are you using babel/register for here?
  • Use require('babel-core/register') with Babel 6
  • @FilipSpiridonov It's now require('babel-register').
  • note it does say not to use this (babel-node) in production
  • it's now require(@babel/register)
  • It doesn't even work without a whole bunch of parameters and jumping through the hoops, so look for the answer below.
  • THANK YOU for posting! You solved my problem i've had for 2 days now! The documentation for babel is terrible!
  • I was trying to use the pure require without the presets, and it was causing a syntax error originating from the .jsx component that I was trying to get onto the server... adding the presets option solved the problem, THANK YOU!!!
  • @SebastianSchürmann could you elaborate on your downvote, I don't quite get what you mean by checking out the dependency to a local folder. He simply wanted to transpile the file the require("@babel/register") is called from, right?
  • Actually, babel/register will compile modules on the fly (but of course not the module it is located in).