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
4 Answers
Reset to default 18You 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
版权声明:本文标题:javascript - TypeError: express is not a function - - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738323513a2074608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论