How do I change these require statements for these modules to use import statements instead?
Asked Answered
B

5

5

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';

Berserker answered 6/5, 2019 at 1:50 Comment(4)
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.Interlunar
@PatrickRoberts, can you add that as your answer and thank you.Berserker
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.Interlunar
@PatrickRoberts, I think the two comments you shared together would make a complete answer.Berserker
C
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.

Cantara answered 6/5, 2019 at 2:36 Comment(0)
C
9

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).

Calliopsis answered 23/5, 2019 at 11:45 Comment(0)
M
1

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

Mann answered 28/10, 2022 at 17:13 Comment(0)
B
1

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';
Brigitta answered 14/2, 2023 at 13:4 Comment(0)
C
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.

Cantara answered 6/5, 2019 at 2:36 Comment(0)
G
0

You can simply add:

"type": "module"

in your package.json file

Gigot answered 22/3, 2023 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.