此模块使用“export =”声明,并且只能在使用“esModuleInterop”标志时与默认导入一起使用

Ant*_*bra 16 node.js typescript

我正在尝试使用一个简单的 API typescriptNodeJS但是当我运行我的代码时出现此错误

“此模块是使用 '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

我的问题是我尝试tsctsc -w index.ts.

但是,正如此处所述/sf/answers/2327082131/如果您tsc使用指定的输入文件运行,它不会tsconfig.json您的项目目录中使用!所以就我而言,我必须使用tsc -w.

  • 这太烦人了,当使用特定文件参数调用时,tsc 会忽略 tsconfig.json。 (2认同)

bas*_*rat 27

你的代码非常好。

使固定

更改 tsconfig 时,有时需要重新启动 IDE 或代码编辑器

  • 如果你使用 VSCode,你可以从命令托盘运行 `TypeScript: Reload Project`。 (4认同)

小智 22

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

  1. 添加到 tsconfig.json->compilerOptions + "moduleResolution": "node"。
  2. 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)

  • 这在 `express()` 上引发了 tsserver 错误:`该表达式不可调用。类型“typeof e”没有调用签名。(tsserver 2349)` (2认同)

atl*_*gim 18

我能够通过将"esModuleInterop"属性设置为tsconfig.json 文件中的对象true内部来解决这个问题。"compilerOptions"

{
  "compilerOptions": {
    "esModuleInterop": true,
    ...
  },
}

Run Code Online (Sandbox Code Playgroud)


小智 7

如果您正在使用节点,请不要忘记设置"moduleResolution": "node"tsconfig.json


Bru*_*que 7

使用 Ts 时,您需要在 tsconfig.json 文件中添加一个允许综合导入的标志,如下所示:

  • 打开 tsconfig.json
  • 添加:{“compilerOptions”:{“jsx”:“react”,“module”:“es6”,“target”:“es6”,“moduleResolution”:“node”,“allowSyntheticDefaultImports”:true}}

希望能帮助到你。干杯!