Skip to content

Cannot import pure ECMAScript modules in rollup.config.js #3443

@haraldrudell

Description

@haraldrudell

package.json:

{
  "type": "module",
  "devDependencies": {
    "rollup": ">=2.0.6 <3",
    "rollup-plugin-thatworks": ">=1.0.3 <2"
  }
}

rollup.config.js:

import {shebang} from 'rollup-plugin-thatworks' // esm import
export default {input: 'rollup.config.js'}

Node.js: v13.6.0

Actual Behavior

yarn && yarn rollup --config
yarn install v1.21.1                                                                                           
warning package.json: No license field                                                                         
warning No license field                                                                                       
[1/4] 🔍  Resolving packages...                                                                                
success Already up-to-date.                                                                                    
✨  Done in 0.18s.
yarn run v1.21.1
warning package.json: No license field                                                                         
$ /opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/.bin/rollup --config                                   
loaded rollup.config.js with warnings                                                                          
(!) Unused external imports                                                                                    
shebang imported from external module 'rollup-plugin-thatworks' but never used                                 
[!] Error: Must use import to load ES Module: /opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/rollup-plugin-thatworks/lib/thatworks.js                                                                                  
require() of ES modules is not supported.                                                                      
require() of /opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/rollup-plugin-thatworks/lib/thatworks.js from /opt/foxyboy/sw/pri/code-samples/rollup2/rollup.config.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.                                                                                                            
Instead rename thatworks.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/rollup-plugin-thatworks/package.json.

    at Module._extensions..js (internal/modules/cjs/loader.js:1160:13)
    at Object.require.extensions. [as .js] (/opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/rol
lup/dist/bin/rollup:1812:17)
    at Module.load (internal/modules/cjs/loader.js:993:32)
    at Function.Module._load (internal/modules/cjs/loader.js:892:14)
    at Module.require (internal/modules/cjs/loader.js:1033:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object. (/opt/foxyboy/sw/pri/code-samples/rollup2/rollup.config.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)
    at Object.require.extensions. [as .js] (/opt/foxyboy/sw/pri/code-samples/rollup2/node_modules/rol
lup/dist/bin/rollup:1809:24)
    at Module.load (internal/modules/cjs/loader.js:993:32)

Expected Behavior

rollup 2.0.6 to be able to use import of ECMAScript modules in rollup.config.js per Node.js v12.0.0+

cause: rollup has an internal transpiler that converts import to require. Node.js ESM then fails

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions