3

If my information is correct i need to import dependencies using import .. from '..' instead of var .. = require('..') in a ES6 application.

So i changed the imports. But i have problem with the import of the cookie-parser receiving the error

Module '\"cookie-parser\"' has no default export."

I changed

var cookieParser = require('cookie-parser');

to

import cookieParser from 'cookie-parser';

How can i solve this problem?

1
  • Was this issue resolved? Commented Aug 13, 2021 at 8:59

4 Answers 4

8

You can use

import cookieParser from 'cookie-parser';

It will work only if you'll also install the types.

npm install --save-dev @types/cookie-parser

You can then use it like so:

app.use(cookieParser());
Sign up to request clarification or add additional context in comments.

Comments

1

One possible approach:

import * as cookieParser from 'cookie-parser';

... following this recommendation.

Comments

1
import cookieParser from 'cookie-parser';

This should work. Id also make sure you are using the babel transpiler for node. If you aren't using babel the module will fail to load properly

Comments

-1

For anyone still facing this issue,

Apart from installing the required types for cookie-parser you also need to enable type: "module" in your package.json file. This way Node knows that you are using the ES6 syntax instead of CommonJS.

Apart from this, also change the module and moduleResolution properties in your tsconfig.json to NodeNext.

{
   "module": "NodeNext"
   "moduleResolution": "nodenext"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.