Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.3.0+] TypeError: Unknown file extension ".json" for /...package.json #3481

Closed
jrgleason opened this issue Apr 1, 2020 · 11 comments
Closed

Comments

@jrgleason
Copy link

jrgleason commented Apr 1, 2020

Expected Behavior

Pre-2.3.0 (IE 2.2.0) I could just run rollup -c rollup.config.mjs and everything would work.

Actual Behavior

After 2.3.0 I get...

[!] TypeError: Unknown file extension ".json" for /.../fail-rollup/package.json

Work Around

Explicitly use version 2.2.0 in my package.json

@lukastaegert
Copy link
Member

Either change your file extension to .js, configure Node to use —experimental-json-modules or use a way to import .json files that is actually supported for .mjs files in Node, cf. https://rollupjs.org/guide/en/#using-untranspiled-config-files

Our assumption is that if you are using .mjs, you are doing so because you want your code to follow Node 13+ ES module semantics, which will now be enforced by loading your config as a Node ES module.

@lukastaegert
Copy link
Member

Here is another way to fix this: https://nodejs.org/api/esm.html#esm_commonjs_json_and_native_modules

@jrgleason
Copy link
Author

Can you speak a little more about that assumption and how that changed from 2.2.0? I guessed that this was a change to better conform to standards but I am confused about why it worked and then stopped? I am using node 14 in 2.2 and it is working fine. So I am just confused about how the assumptions changed. Is there a release note that might help me better understand the motive better?

@jrgleason
Copy link
Author

jrgleason commented Apr 2, 2020

I tried using the code in my project (more advanced than the example I gave)...

node --experimental-json-modules ./node_modules/.bin/rollup --config rollup.config.mjs

But again with no code changes I now get...

[!] SyntaxError: The requested module 'rollup-plugin-eslint' does not provide an export named 'eslint'
file:///.../ui/rollup.config.mjs:4
import { eslint } from "rollup-plugin-eslint";

Which again works fine when I downgrade to 2.2

@jrgleason
Copy link
Author

Seems to be with all the plugins I commented out lint and now I get...

import { terser } from "rollup-plugin-terser";
[!] SyntaxError: The requested module 'rollup-plugin-terser' does not provide an export named 'terser'

@lukastaegert
Copy link
Member

lukastaegert commented Apr 2, 2020

It is all explained in the documentation I linked. Prior to 2.3, all configs were transpiled to CJS before they were executed, now they are not if you are using Node 13+. Before 2.3, we did not try to replicate Node ESM semantics also because they were somewhat unstable at that time. Most plugins, including terser, are only available as CJS. If a Node ES module imports a CJS module, it will only receive a default export. In case of e.g. terser, you could

import terser from 'terser';
...
terser.terser()

@jrgleason
Copy link
Author

jrgleason commented Apr 2, 2020

When I remove both plugins it does seem to work.

@lukastaegert
Copy link
Member

By the way, it is actually not true what I said earlier that those plugins are only available as CJS, they are just not configured to provide an ES module when they are imported from a Node ES module. They would need to use conditional exports to make that work, just like Rollup itself does it now.

@jrgleason
Copy link
Author

Right and just for the record the working example was...

import rollupTerser from "rollup-plugin-terser";
rollupTerser.terser()

Just cause the dependency was wrong and didn't want to confuse people. Or am I wrong and I should be using the terser library directly?

@lukastaegert
Copy link
Member

No, you are right, it should of course import from the plugin 🙄

@wayfarer3130
Copy link

In the config files, you can always do:

import { readFileSync } from "fs";
const pkg = JSON.parse(readFileSync('package.json', {encoding: 'utf8'}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants