如何将这些模块的Require语句更改为使用IMPORT语句?
我希望停止对以下模块使用require()语句,因为节点版本11现在支持ES6,但我找不到任何有关如何将express作为import语句编写的文档:
import express from "express";
const http = require('http');
import bodyParser from 'body-parser';
const morgan = require('morgan');
是否与morgan和http的bodyParser相同?
例如morgan我只看到:
import logger from 'morgan';
对于http,我只看到:
import * as http from 'http';
解决方案
require是模块的主要语法(在节点中)。正如Patrick Roberts提到的,您只能将它们用于.mjs(模块JS)文件。require导入NPM包/节点模块:
const express = require("express");
const http = require("http");
const bodyParser = require("body-parser");
const morgan = require("morgan");
如果您确实希望使用ES6import/export,则需要使用.mjs,如here所述。
相关文章