Typescript not building when "module/package.json".main exists
Bug Report
The compiler throws a SyntaxError when importing a module that includes a package.json pointing to the file.
In my project I have:
foo/
foo.ts
package.json
pages/
index.tsx
...

To reproduce
- Go to with-typescript example
- Create a folder ( e.g. foo )
- Create 2 files: foo.ts and package.json
- Import it at any page
foo.ts
const a: string = 'qwe2'
export default a
package.json
{
"main": "./foo.ts"
}
Expected behavior/code In the previous version it worked without problems. I don't know if this is a next.js related bug or a babel one
Did you only get the issue if you specified a "main" in package.json? I've just tried it out in my project (that uses next-typescript) and I get the issue even if I don't specify a "main"
Same here, no main section is required. It looks like babel ignores typescript plugin altogether.
I managed to get around this issue by using the @babel/preset-typescript preset directly in babel.config.js:
module.exports = function (api) {
api.cache(true)
const presets = [
"next/babel",
[ "@babel/preset-typescript", { "isTSX": true, "allExtensions": true } ]
];
const plugins = [];
return {
presets,
plugins
};
}
I'm not sure whether it's worth updating the next plugin to add these options by default @timneutkens ?
Also, if your next project sits inside a yarn monorepo with a shared babel configuration, you might wish to use https://www.npmjs.com/package/next-plugin-custom-babel-config to supply a reference to the root babel.config.js file.
Any update on this issue?
True, it happens even if main is not specified, but package.json exists.
I got the with-typescript example working with @josephluck solution, but In my project it doesn't seem to work. It fails to compile _app.tsx throwing SyntaxError.
I'm not able to upgrade to the v7 because of this. I'm looking into it.
What I'm getting now is a strange error

Adding @babel/plugin-proposal-export-default-from, as recommended, doesn't solve it.
Hi, I followed the same steps (above) but this issue is not happening on v8.1.1-canary.28 (latest).
Can you try upgrading the version might solve this issue?
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.