tsify + babelify + browserify with threejs modules: SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (9:0)
See original GitHub issueHello there,
I’m trying to use threejs with typescript for a web application. Since I would like to use modules (threejs introduced them in r105), I need to get down to es5 for browserify again for which I thought babel might be a solution. Sadly I still get the error
SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (9:0)
I have the following configuration:
package.json
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babelify": "^10.0.0",
"browserify": "^16.2.3",
"gulp": "^4.0.2",
"tsify": "^4.0.1",
"typescript": "^3.5.2",
.babelrc
{
"presets": [
["@babel/preset-env",
{
"targets": {
"esmodules": true
}
}
],
],
"extensions": [ ".js", ".ts", ".tsx" ]
}
.tsconfig
{
"include": [
"./src/**/*"
],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["types/*"] },
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictNullChecks": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es6"
}
}
gulpfile.js
function buildBundle() {
return browserify({
debug: true
})
.add("./src/main.ts")
.plugin(tsify, { target: 'es6'})
.transform(babelify)
.bundle()
}
File at which the error is triggered
...
import { ResizeSignal } from "../Signals/InputSignals/ResizeSignal";
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; // this is the line
...
private loader: GLTFLoader;
What am I missing? I’m completely stuck.
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (8 by maintainers)
Top Results From Across the Web
'import' and 'export' may appear only with 'sourceType: module ...
In my case, I was getting this error with browserify and babelify when trying to compile JS files that imported TypeScript files, e.g....
Read more >JSM modules for browser double import with TypeScript
Issue: tsify + babelify + browserify with threejs modules: SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (9:0).
Read more >Browserify Transform Error : ParseError: 'import' and 'export ...
In my gulp setup I make use of browserify and babelify like this: ... 'import' and 'export' may appear only with 'sourceType: module'....
Read more >Experts for groupby lodash - Linknovate
We can see below how we can achieve our dog's grouping example using Lodash: import groupBy from 'lodash.groupby'; const byBreed = groupBy(dogs, ...
Read more >Newest 'browserify' Questions - Stack Overflow
Typescript/gulp/browserify: 'import' and 'export' may appear only with 'sourceType: module'. I'm building a project that's using Typescript, Gulp, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Wow, thanks a lot. The require setting indeed solved the problem. Here is the final gulp script:
and the following dependencies:
Sadly the build time now went up from 8 to 15 seconds for me, but at least it works now 😃
Oh, I think babel in v7 also changed how its babelrc resolution works, so a local babelrc is no longer used for modules inside node_modules(?). You might try:
that should not be affected by the new babelrc resolution. you can then use .babelrc only for the transforms you need in your own code.
You can also add https://www.npmjs.com/package/tfilter to optimize things a bit and only transform the three.js modules: