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

Error: 'import' and 'export' may appear only with 'sourceType: "module"' #849

Closed
zhanzhenzhen opened this issue May 15, 2018 · 7 comments · Fixed by #861
Closed

Error: 'import' and 'export' may appear only with 'sourceType: "module"' #849

zhanzhenzhen opened this issue May 15, 2018 · 7 comments · Fixed by #861

Comments

@zhanzhenzhen
Copy link

Ran into this error, then renamed file to ".mjs", but still got this error:

$ npx minify fff.mjs --mangle.topLevel --outFile fff.min.mjs
SyntaxError: unknown: 'import' and 'export' may appear only with 'sourceType: "module"' (1:0)
Consider renaming the file to '.mjs', or setting sourceType:module or sourceType:unambiguous in your Babel config for this file.

> 1 | export let aaa = function(){};
    | ^
  2 |

Environment: Node.js v10.1

@boopathi
Copy link
Member

The default sourceType is script and the Node API exposes it. The CLI doesn't yet have this option.

@matthewp
Copy link

Hey @boopathi I don't believe that fixes it. The error says:

Consider renaming the file to '.mjs', or setting sourceType:module or sourceType:unambiguous in your Babel config for this file.

The file does have the .mjs extension. So the sourceType shouldn't be required, right?

@asakusuma
Copy link

@boopathi I'm running into the same issue. My file extension is .mjs, but I'm still getting the same error. Also, setting sourceType in .babelrc doesn't seem to fix the issue either.

@asakusuma
Copy link

asakusuma commented Jul 17, 2018

My workaround was to directly use babel.transformSync

const minifyPreset = require('babel-preset-minify');
const babel = require('@babel/core');
const fs = require('fs');
const args = process.argv.slice(2);
if (args.length !== 2) {
  throw new Error('Must pass two arguments, input and output filename');
}

const { code } = babel.transformSync(fs.readFileSync(args[0]), {
  minified: true,
  sourceType: "module",
  configFile: false,
  presets: [[minifyPreset]]
});

fs.writeFileSync(args[1], code);

@TianyiShi2001
Copy link

I'm still getting the same issue

@cbuteau
Copy link

cbuteau commented Aug 25, 2020

Same problem...uninstalling...

@el-hackerino
Copy link

Same here still. The file does have the .mjs extension. Sad.

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

Successfully merging a pull request may close this issue.

7 participants