node.js - Acest modul este declarat folosind 'export =', și poate fi utilizat doar cu o importare implicită atunci când se folosește flag-ul 'esModuleInterop'
Încerc să creez un API simplu folosind typescript și NodeJS, dar când rulez codul primesc această eroare
"Acest modul este declarat folosind 'export =', și poate fi folosit doar cu o importare implicită atunci când se utilizează opțiunea 'esModuleInterop'."
Acesta este codul meu: 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"
}
}
Index.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'. */
}
}
Răspuns #1
Codul tău este perfect în regulă.
Repara
Când faci modificări în tsconfig, uneori trebuie să repornești IDE-ul sau editorul de cod 🌹
Alternativ, rulează comanda „Restart TS Server” din paleta de comenzi (CMD + Shift + P)
Răspunsul #2
Folosește :
import * as express from 'express';
Răspunsul #3
Problema mea a fost că am încercat să rulez tsc ca tsc -w index.ts.
Totuși, așa cum se spune aici https://stackoverflow.com/a/33244030/5711655, dacă rulezi tsc cu un fișier de intrare specificat, nu va folosi tsconfig.json din directorul proiectului tău! Așa că, în cazul meu, a trebuit să folosesc tsc -w.
Răspunsul #4
Am reușit să o rezolv setând proprietatea "esModuleInterop" pe true în interiorul obiectului "compilerOptions" din fișierul tsconfig.json.
{
"compilerOptions": {
"esModuleInterop": true,
...
},
}
Răspunsul #5
Am corectat o greșeală similară pentru tine. Folosesc proiectul (vite + vue 3 + ts)
- adăugat în tsconfig.json->compilerOptions + "moduleResolution": "node".
import moment from "moment"laimport * as moment from "moment";
ACTUALIZARE: - și adaugă această setare,
"allowSyntheticDefaultImports": true
și apoi pot folosi, import moment from "moment";
