parcel: ๐ Upgrading from 1.2.0 to 1.3.0 breaks moment.js
Iโm using typescript and when i upgrade parcel from 1.2.0 to 1.3.1 (Iโve tried with 1.3.0 as well) the moment.js
library starts complaining about moment
not being a function.
๐ Configuration (.babelrc, package.json, cli command)
// package.json
{
"scripts": {
"start": "parcel index.html --out-dir temp/"
},
"dependencies": {
"moment": "^2.20.1",
"parcel-bundler": "1.3.0",
"typescript": "^2.6.2"
}
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="./app.ts"></script>
</body>
</html>
//app.ts
import * as moment from 'moment';
console.log( moment() );
No tsconfig.json
๐ค Expected Behavior
I would expect a moment
object to be printed to the console.
๐ฏ Current Behavior
With parcel v1.2.0
the moment
object is printed just fine. But with parcel v1.3.0
or v1.3.1
it throws a TypeError.
b6623a1acd83678d58392c21229a4b42.js:6473 Uncaught TypeError: moment is not a function
at Object.require.2.moment (b6623a1acd83678d58392c21229a4b42.js:6473)
at newRequire (b6623a1acd83678d58392c21229a4b42.js:41)
at require.4 (b6623a1acd83678d58392c21229a4b42.js:66)
at b6623a1acd83678d58392c21229a4b42.js:71
๐ฆ Context
Gist of all files used to recreate bug: https://gist.github.com/Olian04/ce4e23c11a17d60c9a39247a879e54c7
๐ Your Environment
Software | Version(s) |
---|---|
Parcel | 1.2.0 & 1.3.0 & 1.3.1 |
Node | 9.3.0 |
npm/Yarn | 5.5.1 / 1.3.2 (Iโve tried with both) |
Operating System | Ubuntu 17.04 |
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (6 by maintainers)
Iโm also using typescript in my projects now. I have experienced the same error as yours, Typescript is upgraded to version
2.7
, it supportses6 module
. so I solved the same problem as yours, try version up for typescript, and add tsconfig.jsonI solved moment js problem. try this! ๐
@brandon93s
import moment from 'moment';
isnโt valid ts, sincemoment
doesnโt provide a default export.Unsure yet exactly why the default behavior has changed, but the following is a workaround:
https://momentjs.com/docs/#/use-it/typescript/
@Olian04 ~
moment
is exported asdefault
in itโs ES6 form, see the code (referenced byjsnext:main
). The problem here is that themain
field points to a UMD module, which usesmodule.exports
.~edit: Didnโt understood you were talking about ts. Yeah
allowSyntheticDefaultImports
is the way to go. Parcel uses Babel which will automatically find if a module is ES6 or CommonJS and export the correct value accordingly, so itโs safe to use :