首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >此模块使用“This =”声明,并且只能在使用“esModuleInterop”标志时用于默认导入。

此模块使用“This =”声明,并且只能在使用“esModuleInterop”标志时用于默认导入。
EN

Stack Overflow用户
提问于 2020-06-09 00:26:43
回答 11查看 68.2K关注 0票数 76

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

“此模块是使用‘esModuleInterop =’声明的,并且只能在使用'esModuleInterop‘标志时与默认导入一起使用。

这是我的代码: package.json

代码语言:javascript
运行
复制
    {
  "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"
  }
}

Index.ts

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
    {
  "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'. */
  }
}
EN

Stack Overflow用户

发布于 2022-04-15 07:53:28

我为自己修正了类似的错误。我使用项目(vite + vue 3+ ts)

在tsconfig.json上添加的"node".

  • import moment from "moment"

  • ->选项+ "moduleResolution": to import * as moment from "moment";

UPD:-加上这个设置,

代码语言:javascript
运行
复制
"allowSyntheticDefaultImports": true

然后我可以用,import moment from "moment";

票数 9
EN
查看全部 11 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62273153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档