dknaack
dknaack

Reputation: 60466

How to use expressjs in a NodeJS/ES6/Typescript app

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?

Upvotes: 3

Views: 6296

Answers (4)

Zero
Zero

Reputation: 1899

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"
}

Upvotes: -1

Udi Mazor
Udi Mazor

Reputation: 1826

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());

Upvotes: 8

Robbie
Robbie

Reputation: 45

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

Upvotes: 1

raina77ow
raina77ow

Reputation: 106385

One possible approach:

import * as cookieParser from 'cookie-parser';

... following this recommendation.

Upvotes: 1

Related Questions