![](/img/trans.png)
[英]Newrelic.js not working with Typescript NodeJs Express App
[英]Express not working with Typescript in NodeJS app
我正在学习打字稿,我已经按照教程使用它并逐字表达 api 应用程序,但我收到以下错误:
D:\Development\Node\Ed\TypeScript\src\app.ts:5
const app: Application = express();
^
TypeError: express_1.default is not a function
at Object.<anonymous> (D:\Development\Node\Ed\TypeScript\src\app.ts:5:33)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\index.ts:814:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\index.ts:817:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at main (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\bin.ts:226:14)
我不知道如何解决,希望得到任何指示
app.ts 文件:
import express, { Application, Request, Response, NextFunction } from 'express';
const app: Application = express();
app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.send('hello');
});
app.listen(5000, () => console.log('Server running'));
tsconfig.json:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"removeComments": true, /* Do not emit comments to output. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
}
}
您需要更改导入文件的方式。 它应该是:
import * as express from "express";
import { Application, Request, Response, NextFunction } from 'express';
我按照本网站上的说明进行操作
它工作正常。
您是否将 @types/express 作为开发依赖项安装?
npm i --save-dev @types/express
我的 index.ts:
import express, {Application} from 'express';
const app: Application = express()
app.listen(5000, ()=>{
console.log('server runnning')
})
我的 tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"src/**/*"
]
}
我的 tslint.json:
{
"defaultSeverity": "warning",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"trailing-comma": [ false ]
},
"rulesDirectory": []
}
最后,我的 package.json
{
"name": "tsnjs",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
"build": "tsc",
"prestart": "npm run build",
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"@types/express": "^4.17.2",
"tslint": "^6.0.0",
"typescript": "^3.8.2"
}
}
节点 -v v13.9.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.