Parcel: Tree shaking fails with no default export (d3-transition library)

Created on 15 Nov 2019  路  6Comments  路  Source: parcel-bundler/parcel

馃悰 Bug Report

When trying to build a project that includes d3-transition with --experimental-scope-hoisting, Parcel crashes with the :

馃帥 Configuration (.babelrc, package.json, cli command)

package.json:

{
  "name": "test_repo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "dependencies": {
    "d3-transition": "^1.2.0",
    "parcel-bundler": "^1.12.4"
  }
}

Command to run: parcel build --experimental-scope-hoisting test.js

馃 Expected Behavior

The project builds normally with no errors (this happens when the --experimental-scope-hoisting flag is removed, but should work with the flag, too)

馃槸 Current Behavior

Parcel crashes with the following error:

yarn parcel build --experimental-scope-hoisting test.js
yarn run v1.19.0
$ /home/jake/temp/test_repo/node_modules/.bin/parcel build --experimental-scope-hoisting test.js
?  node_modules/d3-color/src/lab.js does not export 'default'
    at replaceExportNode (/home/jake/temp/test_repo/node_modules/parcel-bundler/src/scope-hoisting/concat.js:55:13)
    at ReferencedIdentifier (/home/jake/temp/test_repo/node_modules/parcel-bundler/src/scope-hoisting/concat.js:342:20)
    at newFn (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/visitors.js:216:17)
    at NodePath._call (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/path/context.js:42:17)
    at NodePath.visit (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitSingle (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/context.js:84:19)
    at TraversalContext.visit (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/context.js:140:19)
    at Function.traverse.node (/home/jake/temp/test_repo/node_modules/@babel/traverse/lib/index.js:84:17)
error Command failed with exit code 1.

馃敠 Context

Would really like to be able to use tree shaking.

馃捇 Code Sample

test.js:

var x = require('d3-transition');

console.log(x)

package.json and test.js are all that are required to reproduce the bug.

Steps:

  • Place package.json and test.js in a folder
  • Install node modules
  • Run parcel build --experimental-scope-hoisting test.js

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.12.4 |
| Node | 12.11.1 (also occurs on 10.17.x) |
| Yarn | 1.19.0 |
| Operating System | Arch Linux |

Bug Stale 馃尦 Tree Shaking

Most helpful comment

I ran into 'no default export' error too (despite it having a default export in the module).
Error: ../node_modules/react-share/es/FacebookShareButton.js does not export 'default'

Fixed by upgrading to Parcel V2, removing the --experimental-scope-hoisting flag from the parcel build command, and fixing a few issues.

1. Upgrade V1 to V2
In terminal ran:
npm uninstall parcel-bundler
From Parcel v2 README:
npm install --save-dev parcel@next

2. Remove --experimental-scope-hoisting flag from parcel build
In package.json, for npm run build command, I removed --experimental-scope-hoisting:
"prod": "parcel build public/index.html && cd ./server && tsc && NODE_ENV=production node ./dist/index.js",

3. Fixed issues V1 -> V2
Fixed .png imports error:
Added .parcelrc file in the root described in V2 README
npm install --save-dev @parcel/transformer-raw
{ "extends": "@parcel/config-default", "transformers": { "*.png": ["@parcel/transformer-raw"] } }

Fixed this error:
"description": "", "main": "index.js", ^^^^^^^^^^ Did you mean "index.html"?

By following this fix, in my package.json, I removed "main": "index.js".

Ran npm run prod once again...
And tada! It bundled with no errors + epic tree shaking ~12MB bundle to ~1.5MB ! Thank you Parcel!

All 6 comments

I'm not sure if this is related, but I get a similar error when trying to use React Spring with tree shaking.

脳  ..\node_modules\react-spring\web.js does not export 'useTransition'
    at replaceExportNode (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\parcel-bundler\src\scope-hoisting\concat.js:55:13)
    at ReferencedIdentifier (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\parcel-bundler\src\scope-hoisting\concat.js:342:20)
    at newFn (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\visitors.js:216:17)
    at NodePath._call (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\path\context.js:55:20)
    at NodePath.call (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\path\context.js:42:17)
    at NodePath.visit (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\path\context.js:90:31)
    at TraversalContext.visitQueue (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\context.js:112:16)
    at TraversalContext.visitSingle (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\context.js:84:19)
    at TraversalContext.visit (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\context.js:140:19)
    at Function.traverse.node (C:\Users\19haho\Downloads\GitHub\zeiw\node_modules\@babel\traverse\lib\index.js:84:17)
error Command failed with exit code 1.

Came here to report this. Happens to a bunch of d3 modules with tree shaking enabled.

Got a similar error:

$ ./scripts/build.sh
馃毃  ../node_modules/d3-interpolate/src/cubehelix.js does not export 'cubehelixLong'
    at replaceExportNode (/home/travis/build/DemystData/cfola/node_modules/parcel-bundler/src/scope-hoisting/concat.js:55:13)
    at ReferencedIdentifier (/home/travis/build/DemystData/cfola/node_modules/parcel-bundler/src/scope-hoisting/concat.js:342:20)
    at newFn (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/visitors.js:216:17)
    at NodePath._call (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/path/context.js:42:17)
    at NodePath.visit (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitSingle (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/context.js:84:19)
    at TraversalContext.visit (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/context.js:140:19)
    at Function.traverse.node (/home/travis/build/DemystData/cfola/node_modules/@babel/traverse/lib/index.js:84:17)

Another one:

馃毃 ../node_modules/date-fns-tz/esm/utcToZonedTime/index.js does not export 'default' at replaceExportNode (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/parcel-bundler/src/scope-hoisting/concat.js:55:13) at ReferencedIdentifier (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/parcel-bundler/src/scope-hoisting/concat.js:342:20) at newFn (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/visitors.js:220:17) at NodePath._call (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/path/context.js:55:20) at NodePath.call (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/path/context.js:42:17) at NodePath.visit (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/path/context.js:90:31) at TraversalContext.visitQueue (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/context.js:112:16) at TraversalContext.visitSingle (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/context.js:84:19) at TraversalContext.visit (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/context.js:140:19) at Function.traverse.node (/home/tony/repos/Hatch/admin-hatchapp-web-client/node_modules/@babel/traverse/lib/index.js:84:17)

I ran into 'no default export' error too (despite it having a default export in the module).
Error: ../node_modules/react-share/es/FacebookShareButton.js does not export 'default'

Fixed by upgrading to Parcel V2, removing the --experimental-scope-hoisting flag from the parcel build command, and fixing a few issues.

1. Upgrade V1 to V2
In terminal ran:
npm uninstall parcel-bundler
From Parcel v2 README:
npm install --save-dev parcel@next

2. Remove --experimental-scope-hoisting flag from parcel build
In package.json, for npm run build command, I removed --experimental-scope-hoisting:
"prod": "parcel build public/index.html && cd ./server && tsc && NODE_ENV=production node ./dist/index.js",

3. Fixed issues V1 -> V2
Fixed .png imports error:
Added .parcelrc file in the root described in V2 README
npm install --save-dev @parcel/transformer-raw
{ "extends": "@parcel/config-default", "transformers": { "*.png": ["@parcel/transformer-raw"] } }

Fixed this error:
"description": "", "main": "index.js", ^^^^^^^^^^ Did you mean "index.html"?

By following this fix, in my package.json, I removed "main": "index.js".

Ran npm run prod once again...
And tada! It bundled with no errors + epic tree shaking ~12MB bundle to ~1.5MB ! Thank you Parcel!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

medhatdawoud picture medhatdawoud  路  3Comments

philipodev picture philipodev  路  3Comments

donaldallen picture donaldallen  路  3Comments

humphd picture humphd  路  3Comments

Niggler picture Niggler  路  3Comments