Parcel 2: API Could not find a .parcelrc

🐛 bug report

When trying to use the API, if you import another file which contains imported packages, Parcel throws an Could not find a .parcelrc error.

🤔 Expected Behavior

It should work as when trying to bundle the same file from the CLI

😯 Current Behavior

× Could not find a .parcelrc
    at loadParcelConfig (C:\dev\parcelrc-error\node_modules\@parcel\core\lib\loadParcelConfig.js:54:11)
    at async ConfigLoader.loadParcelConfig (C:\dev\parcelrc-error\node_modules\@parcel\core\lib\ConfigLoader.js:68:35)
    at async PromiseQueue._runFn (C:\dev\parcelrc-error\node_modules\@parcel\utils\lib\PromiseQueue.js:94:7)
    at async PromiseQueue._next (C:\dev\parcelrc-error\node_modules\@parcel\utils\lib\PromiseQueue.js:81:5)

💻 Code Sample

https://github.com/Banou26/parcel-bug-3749

const Parcel = require('@parcel/core').default
const config = require('@parcel/config-default')

new Parcel({
  // defaultConfig: config,
  entries: ['./src/test.js'],
  targets: {
    test: {
      distDir: 'dist',
      browsers: ['last 1 Chrome versions']
    }
  },
  sourceMaps: true,
  minify: true,
  scopeHoist: true
}).watch((err, build) => {
  if (err) console.error(err)
  console.log('build successful')
})

1 possible answer(s) on “Parcel 2: API Could not find a .parcelrc

  1. You need to pass a filename as well (so Parcel knows from where to resolve the plugin names in the config):

    let defaultConfig: ParcelConfigFile = await packageManager.require(
    ‘@parcel/config-default’,
    __filename
    );
    let parcel = new Parcel({
    entries,
    packageManager,
    defaultConfig: {
    defaultConfig,
    filePath: (await packageManager.resolve(
    ‘@parcel/config-default’,
    __filename
    )).resolved
    },

    so:

    const Parcel = require('@parcel/core').default
    const config = require('@parcel/config-default')
    
    new Parcel({
      defaultConfig: {
        ...config
        filePath: require.resolve("@parcel/config-default"),
      }
      entries: ['./src/test.js'],
      targets: {
        test: {
          distDir: 'dist',
          browsers: ['last 1 Chrome versions']
        }
      },
      sourceMaps: true,
      minify: true,
      scopeHoist: true
    }).watch((err, build) => {
      if (err) console.error(err)
      console.log('build successful')
    })