javascriptecmascript-6babeljsbrowserifybabelify

ParseError: 'import' and 'export' may appear only with 'sourceType: module', browserify


I am using django-compressor and I want to es5/es6 features on my project. I am setting the pre-compiler option as follow:

NODE_MODULES = BASE_DIR / 'node_modules'

COMPRESS_PRECOMPILERS = (
    ('text/javascript',
     'browserify {{infile}} -t {node_modules}/babelify  -o {{outfile}} --presets {node_modules}/@babel/preset-env'.format(node_modules=NODE_MODULES)),
    ('module',
     'browserify {{infile}} -t {node_modules}/babelify -o {{outfile}} --presets {node_modules}/@babel/preset-env'.format(node_modules=NODE_MODULES)),
)

As you noticed browserify is installed globally and babelify and @babel/preset-env are installed locally inside the project.

From the docs @babel/preset-env now are the recommended presets to use, but I am getting the following error:

ParseError: 'import' and 'export' may appear only with 'sourceType: module'

According to my understanding, when using babel/preset-env, you do not need any other preceding presets e.g. babel-preset-es2015 and others, correct? if yes what are the possible reasons for this issue?


Solution

  • Although the command was working fine, it seems there was an syntax errors on how I specify the presets, anyway I have fixed like that:

    COMPRESS_PRECOMPILERS = (
        ('text/javascript',
         'browserify {{infile}} -o {{outfile}} -t [ {node_modules}/babelify --presets [ {node_modules}/@babel/preset-env ] ]'.format(node_modules=NODE_MODULES_LOCAL)),
        ('module',
         'browserify {{infile}} -o {{outfile}} -t [ {node_modules}/babelify --presets [ {node_modules}/@babel/preset-env ] ]'.format(node_modules=NODE_MODULES_LOCAL)),
    
    )