Not able to import proj4 in angular project

1.8k Views Asked by At

When I am trying to import proj4 in my angular project I am getting an error

Error: export 'defs' (imported as 'proj4') was not found in 'proj4' (possible exports: default)

I am importing as

import * as proj4 from 'proj4';

Though I have installed the proj4 as

npm install --save @types/proj4

How can I solve this issue? Am I missing something here ?

4

There are 4 best solutions below

0
Christian Fuchs On

According to https://github.com/proj4js/proj4js/issues/247, for Typescript:

  • the import statement should be import proj4 from 'proj4';
  • and compiler option allowSyntheticDefaultImports: true must be set in tsconfig.json

This solved the issue for me for TypeScript 4.8 and Proj4 2.8.

0
d1fficult On

This import statement import * as proj4 from 'proj4'; used to work before year 2017 for v2.4 of proj4.

To use it for 2.4 and above, import it this way: -

import proj4 from 'proj4';

Also, you might need to do the following changes to your TypeScript compiler i.e. inside tsconfig.json: -

"allowSyntheticDefaultImports": true

These changes should do it.

0
Rob On

I am using Angular 17 and I just ran the recommended action and it appears to have fixed the issue:

npm i --save-dev @types/proj4
0
Timothée Billiet On

The solution for me was to have both proj4 and @types/proj4 installed. Rather silly but if it works it works.

npm i proj4 --save
npm i @types/proj4 --save