'This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag
I'm trying to make a simple API using typescript
and NodeJS
but when I run my code I get this error
"This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag."
This is my code: 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'. */
}
}
Solution 1:[1]
Your code is perfectly fine.
Fix
When you make changes to tsconfig, sometimes you need to restart your IDE or Code Editor ?
Solution 2:[2]
Use:
import * as express from 'express';
Solution 3:[3]
My problem was that I tried to run tsc
as tsc -w index.ts
.
However, as said here https://stackoverflow.com/a/33244030/5711655 if you run tsc
with a specified input file, it won't use the tsconfig.json
in your project directory! So in my case I had to use tsc -w
.
Solution 4:[4]
Don't forget to set "moduleResolution": "node"
in the tsconfig.json
if you are using node.
Solution 5:[5]
FWIW, I found that switching from
import * as blah from 'blah-lib';
To
const blah = require('blah-lib');
Fixed the issue at compile time.
Solution 6:[6]
I was wondering a lot but a friend helped me:
- You need to create a command inside of "scripts" part on package.json
Package.json
{
"name": "api-proyecto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
**"build-dev": "tsc -w"**
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1"
}
}
To run your script you need to type the following line
npm i run build-dev (because it's the name of our command)
Finally in another powershell launch node normally. For Example:
node .\dist\index.js
Solution 7:[7]
I fixed similar mistake for yourself. I use project (vite + vue 3 + ts)
- added on tsconfig.json->compilerOptions + "moduleResolution": "node".
import moment from "moment"
toimport * as moment from "moment";
UPD:- and add this setting,
"allowSyntheticDefaultImports": true
and then I can use,
import moment from "moment";
Solution 8:[8]
I solved the problem by compiling the whole file instead of compiling individual files.
Just run the following command to compile the whole file in a go
npx tsc
This should solve your problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cavdy |
Solution 2 | Ayrton Everton |
Solution 3 | jeti |
Solution 4 | glennsl |
Solution 5 | Joseph Lust |
Solution 6 | Antonio Labra |
Solution 7 | Anil Nivargi |
Solution 8 | citizen4 |