Ant*_*bra 16 node.js typescript
我正在尝试使用一个简单的 API typescript,NodeJS但是当我运行我的代码时出现此错误
“此模块是使用 'export =' 声明的,并且只能在使用 'esModuleInterop' 标志时与默认导入一起使用。”
这是我的代码:package.json
{
"name": "API-Proyecto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1"
},
"devDependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6",
"nodemon": "^2.0.4"
}
}
Run Code Online (Sandbox Code Playgroud)
索引.ts
import express from 'express';
import { Request, Response } from 'express';
import cors from 'cors';
const app = express();
app.use(express.urlencoded({extended: true}));
app.use(express.json);
app.use(cors({origin: true, credentials: true}));
app.get('/api/auth/testing', (req: Request, res: Response) =>{
res.status(200).json({
ok : true,
msg : 'hi from my method'
});
});
app.listen(3000, () => {console.log('Server running on 3000' )});
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
Run Code Online (Sandbox Code Playgroud)
flo*_*w3r 34
我的问题是我尝试tsc以tsc -w index.ts.
但是,正如此处所述/sf/answers/2327082131/如果您tsc使用指定的输入文件运行,它不会在tsconfig.json您的项目目录中使用!所以就我而言,我必须使用tsc -w.
bas*_*rat 27
你的代码非常好。
更改 tsconfig 时,有时需要重新启动 IDE 或代码编辑器
小智 22
我为自己修正了类似的错误。我使用项目(vite + vue 3 + ts)
import moment from "moment"到import * as moment from "moment";UPD:- 并添加此设置,
"allowSyntheticDefaultImports": true
Run Code Online (Sandbox Code Playgroud)
然后我可以使用,
import moment from "moment";
小智 18
用:
import * as express from 'express';
Run Code Online (Sandbox Code Playgroud)
atl*_*gim 18
我能够通过将"esModuleInterop"属性设置为tsconfig.json 文件中的对象true内部来解决这个问题。"compilerOptions"
{
"compilerOptions": {
"esModuleInterop": true,
...
},
}
Run Code Online (Sandbox Code Playgroud)
使用 Ts 时,您需要在 tsconfig.json 文件中添加一个允许综合导入的标志,如下所示:
希望能帮助到你。干杯!
| 归档时间: |
|
| 查看次数: |
14735 次 |
| 最近记录: |