admin管理员组

文章数量:1186061

Express code "const app = express()" does not work!!

** Hi, I have some problem here! My console return this error, but i follow the best way! please someone give me a light.

server.js archive:

import * as express from "express";
import * as mongoose from "mongoose";
//inciando o app
const app = express();

//iniciando o banco de dados
mongoose.connection('mongodb://localhost:27017/program01', {useNewUrlParser: true});

//primeira rota
app.get('/',(req,res)=>{
    res.send('Hello World')
})

app.listen(3000)


==============================
console: 

*[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
[nodemon] restarting due to changes...
(node:12020) ExperimentalWarning: The ESM module loader is experimental.
[nodemon] starting `node server.js`
(node:8472) ExperimentalWarning: The ESM module loader is experimental.
file:///C:/Users/USUARIO/Documents/Project%20Web/program01/server.js:4
const app = express();
            ^

TypeError: express is not a function
    at file:///C:/Users/USUARIO/Documents/Project%20Web/program01/server.js:4:13        
    at ModuleJob.run (internal/modules/esm/module_job.js:137:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)
[nodemon] app crashed - waiting for file changes before starting...* ```


another's archives :
**node_modules**
**package.json**
**package-lock.json**
**yarn-error.log**

Express code "const app = express()" does not work!!

** Hi, I have some problem here! My console return this error, but i follow the best way! please someone give me a light.

server.js archive:

import * as express from "express";
import * as mongoose from "mongoose";
//inciando o app
const app = express();

//iniciando o banco de dados
mongoose.connection('mongodb://localhost:27017/program01', {useNewUrlParser: true});

//primeira rota
app.get('/',(req,res)=>{
    res.send('Hello World')
})

app.listen(3000)


==============================
console: 

*[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
[nodemon] restarting due to changes...
(node:12020) ExperimentalWarning: The ESM module loader is experimental.
[nodemon] starting `node server.js`
(node:8472) ExperimentalWarning: The ESM module loader is experimental.
file:///C:/Users/USUARIO/Documents/Project%20Web/program01/server.js:4
const app = express();
            ^

TypeError: express is not a function
    at file:///C:/Users/USUARIO/Documents/Project%20Web/program01/server.js:4:13        
    at ModuleJob.run (internal/modules/esm/module_job.js:137:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)
[nodemon] app crashed - waiting for file changes before starting...* ```


another's archives :
**node_modules**
**package.json**
**package-lock.json**
**yarn-error.log**
Share Improve this question asked Jun 22, 2020 at 19:28 Isaac BandimIsaac Bandim 1011 gold badge1 silver badge3 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 18

You need to use a default import for express, i.e.

import express from 'express';

instead of

import * as express from 'express';

add to tsconfig.json

"allowSyntheticDefaultImports": true

and use import like this

import express from 'express'

it works for my

ts-node-esm src/server.ts 

or

node --loader=ts-node/esm --inspect src/server.ts

import is an ES6 feature, it hasn't yet been fully supported by Node.js you should use require

const express = require('express')
const app = express()

Got the same, I used require instead if import. And don't forget to write require, like this:

const express = require('express');

Then you can check it in the console.

本文标签: javascriptTypeError express is not a function Stack Overflow