Does NodeJS support import style module loading?

I am looking at a NodeJS project that is downloading from GitHub . It has a main file server.jsthat uses the ES6 module import syntax as follows:

import express from 'express';
import bodyParser from 'body-parser';
import fs from 'fs';
import { search } from './lib/words';

I have NodeJS version 4.6.0 installed, which is pretty old, and I don't think it supports this syntax. Instead, it should look like this:

var express = require(express)
var bodyParser = require('body-parser')
...

However, I can start this project correctly without errors, which, I think, shows that NodeJS supports this syntax, but the NodeJS documentation never indicates such a module syntax. What is the reason that we can use it here? Thank you for your help.

+4
source share
1

npm start start script package.json, start.js .

start.js babel-register, ES6 ( ) ES5 JavaScript, Node . .babelrc , es2015 preset ES2015 (ES6) ES5 JS.

, , transform-es2015-modules-commonjs, import require, .

+7

Source: https://habr.com/ru/post/1662692/


All Articles