Error: Should not import the named export 'version' (imported as 'version')
Solution 1
As noted in the comments, it is not recommended to expose your package.json file like this.
Change the following
import { version } from '../../package.json';
to something like
import * as packageInfo from '../../package.json';
And then change your access from something like
version,
or
version: version,
to
version: packageInfo.version,
Solution 2
You should also add
"allowSyntheticDefaultImports": true,
to the compileroptions in the tsconfig.json
Solution 3
I solved my issue with the following:
import packageInfo from './package.json';
version = packageInfo.version;
Solution 4
With latest version of create react app
, following syntax works:
import rData from './registration-form.json';
Solution 5
How about
const appVersion = require('./package.json').version;
?
Using this we are not actually shipping the whole package.json but just bringing in the version from it.
Related videos on Youtube
![Muhammad Kamal](https://lh3.googleusercontent.com/-gBSV3chAN-w/AAAAAAAAAAI/AAAAAAAAAHI/55YEqZ54D4E/photo.jpg?sz=256)
Muhammad Kamal
Updated on April 05, 2022Comments
-
Muhammad Kamal about 2 years
I have an ejected create-react-app project. I am getting this error after updating it to webpack 5. It was working fine with webpack v4.41.5
OS: MacOS Catalina 10.15.7
node: v10.23.0Error: Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon).
-
james emanon over 3 yearsDid you find a solution?
-
Muhammad Kamal about 3 yearsyes. The error was because of the following line in the code.
const packageJson = require('../package.json')
. -
Pallav Chanana over 2 yearsSolution here stackoverflow.com/questions/70298948/…
-
Wesley over 2 yearsHere's another option stackoverflow.com/questions/41123631/…
-
Muhammad Kamal over 2 years@karel I am writing this comment in response to your edit. The webpack config is not useful to the question. The only thing that matters is the upgrade to webpack 5 form webpack 4.
-
karel over 2 yearsI removed the webpack config.
-
-
amaster about 3 yearsBut doesn't this pose a security risk and expose the entire package.json to the client?
-
Splaktar almost 3 yearsThat question seems unrelated to the question above. My answer simply addresses the question, not whether it is a good practice or secure in all contexts. If you have private/secret contents in your
package.json
and don't want them to ever be available in your JS code, then don't do this. How to do this in a more secure way should be covered in a separate question. -
Danial Delkhosh almost 3 yearsimport * as packageInfo from '../../package.json'; version: packageInfo.version,
-
Muhammad Kamal almost 3 yearsWelcome to StackOverflow. What you are suggesting can be confusing because the imported object will contain the complete json object from the package.json file and not just the version. I think a better alternative would be:
import packagejson from '../../package.json';
const {version} = packageJson;
-
Jey DWork almost 3 years@Splaktar even without secrets in the
package.json
I find this extremely unsecure. Most devs and maintainers, even with experience, will not treat thepackage.json
as something in the public domain. And with an implementation like that 'hidden' in the codebase really painful shots in the foot lie ahead. Especially so since this error now comes up for Angular 12 which is client-side technology. So a clearly visible HEADS-UP would be fitting imho. -
Alexey Grinko over 2 yearsAgree with @JeyDWork, this solution should be flagged as potential security risk. If your
package.json
doesn't contain confidential data now, who will guarantee that other developers won't put it there years later, without knowing that it is exposed to production? And even without any secrets, normally, a business doesn't want to expose information about itsdependencies
anddevDependencies
from thepackage.json
to everybody - it makes it easier to find vulnerabilities and penetrate the application. -
Patrick Kenny over 2 yearsFollow-up question on how to do this securely here: stackoverflow.com/questions/70298948/…
-
Nuryagdy Mustapayev over 2 yearsI imported JSON files in the unit testing codes in my Angular project. I started getting this error after upgrading the Angular version to v12. In my test code, I already was importing in the correct way as described in the accepted answer, but I was getting this error. Changing to
require
is solved for me. thanks. -
Nuryagdy Mustapayev over 2 yearstried on Angular v12 (after upgrading from v9). Did not work in my case. tried to add both to
tsconfig.json
andtsconfig.app.json
-
Musma over 2 years@NuryagdyMustapayev this worked for me:
import * as packageInfo from '../../package.json';
and rename the variable instead of accesingversion
directlysomeName = packageInfo
-
Breakpoint25 over 2 yearsThis does not fix the issue, you will now get this Warning instead.
Should not import the named export 'version' (imported as 'packageJson') from default-exporting module (only default export is available soon)
-
jarodsmk about 2 yearsThis syntax is the correct syntax to use
-
Andrei about 2 yearsmaybe you also have to add "resolveJsonModule": true, "esModuleInterop": true to your tsconfig.json to be able to apply default import for json
-
Andrei about 2 yearsmay be worth to take a look at stackoverflow.com/questions/49996456/…
-
Antajo Paulson about 2 yearsUpdate the import statement as
import packageInfo from '../../package.json';
@Breakpoint25 -
Pieterjan about 2 yearsThis together with
"resolveJsonModule": true, "allowSyntheticDefaultImports": true