该模块是使用 'export =' 声明的,并且只能在使用 'esModuleInterop' 标志时与默认导入一起使用

新手上路,请多包涵

我正在尝试使用 typescriptNodeJS 制作一个简单的 API,但是当我运行我的代码时,我得到了这个错误

“此模块是使用 ‘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"
  }
}

索引.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' )});

tsconfig.json

     {
  "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'. */
  }
}

原文由 Antonio Labra 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
2 个回答

您的代码非常好。

使固定

当您对 tsconfig 进行更改时,有时您需要重新启动 IDE 或代码编辑器🌹

原文由 basarat 发布,翻译遵循 CC BY-SA 4.0 许可协议

采用:

 import * as express from 'express';

原文由 Ayrton Everton 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏