4

I am looking to stop using require() statements for the following modules since Node version 11 now supports ES6, but I cannot locate any documentation on how to write the following except express as an import statement:

import express from "express";
const http = require('http');
import bodyParser from 'body-parser';
const morgan = require('morgan');

Is it the same as with bodyParser for morgan and http?

For example for morgan I have only seen:

import logger from 'morgan';

and for http I have only seen:

import * as http from 'http';

4
  • 1
    ES6 imports using the experimental flag in node.js is only supported for files with the .mjs extension. If the package doesn't support ES6 module syntax then you can't import it that way. May 6, 2019 at 2:25
  • @PatrickRoberts, can you add that as your answer and thank you.
    – Daniel
    May 6, 2019 at 2:29
  • 1
    It's not really a complete answer. You can still make it work using bundlers or module loaders like rollup, webpack, babel, etc. but doing that for server-side is very atypical. May 6, 2019 at 2:31
  • @PatrickRoberts, I think the two comments you shared together would make a complete answer.
    – Daniel
    May 6, 2019 at 2:35

5 Answers 5

6

Using Node v12.2.0 I can import all standard modules like this:

import * as Http from 'http'
import * as Fs from 'fs'
import * as Path from 'path'
import * as Readline from 'readline'
import * as Os from 'os'

Versus what I did before:

const
  Http = require('http')
  ,Fs = require('fs')
  ,Path = require('path')
  ,Readline = require('readline')
  ,Os = require('os')

Any module that is an ECMAScript module can be imported without having to use an .mjs extension as long as it has this field in its package.json file:

"type": "module"

So make sure you put such a package.json file in the same folder as the module you're making.

And to import modules not updated with ECMAScript module support you can do like this:

// Implement the old require function
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

// Now you can require whatever
const
  WebSocket = require('ws')
  ,Mime = require('mime-types')
  ,Chokidar = require('chokidar')

And of course, do not forget that this is needed to actually run a script using module imports:

node --experimental-modules my-script-that-use-import.js

And that the parent folder needs this package.json file for that script to not complain about the import syntax:

{
  "type": "module"
}

If the module you want to use has not been updated to support being imported using the import syntax then you have no other choice than using require (but with my solution above that is not a problem).

0

require is the primary syntax (in Node) for modules. As Patrick Roberts mentioned, you can only use them for a .mjs (module JS) file. require is how you import an NPM package/Node module:

const express = require("express");
const http = require("http");
const bodyParser = require("body-parser");
const morgan = require("morgan");

If you do wish to use ES6 import/export, you need to use .mjs, as stated here.

0
0

3 simple steps:

1- in your package.json file add:

"type": "module",

enter image description here

2- use this expression at the end of the file to export the function you want to import in other files:

export default sampleFunctionNameToExport

enter image description here

3- change require to import in the files you need to import what you have exported. enter image description here

0

I think it helps to understand what's happening:

From the module "http", take the default exported object, and bind it to the constant "http":

const http = require('http')

From the module "http", import the object "http":

import http from 'http';
0

You can simply add:

"type": "module"

in your package.json file

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.