Isaac Bandim
Isaac Bandim

Reputation: 101

TypeError: express is not a function -

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**

Upvotes: 10

Views: 14762

Answers (4)

Nail Gumerov
Nail Gumerov

Reputation: 17

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.

Upvotes: -1

SV0505
SV0505

Reputation: 91

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

Upvotes: 5

vignesh
vignesh

Reputation: 2525

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

Upvotes: 4

px1mp
px1mp

Reputation: 5372

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

import express from 'express';

instead of

import * as express from 'express';

Upvotes: 18

Related Questions