next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Typescript not building when "module/package.json".main exists

Open vinipace opened this issue 8 years ago • 7 comments

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
...

image

To reproduce

  1. Go to with-typescript example
  2. Create a folder ( e.g. foo )
  3. Create 2 files: foo.ts and package.json
  4. 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

vinipace avatar Sep 20 '18 01:09 vinipace

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"

josephluck avatar Sep 20 '18 10:09 josephluck

Same here, no main section is required. It looks like babel ignores typescript plugin altogether.

ex3ndr avatar Sep 25 '18 03:09 ex3ndr

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.

josephluck avatar Sep 25 '18 10:09 josephluck

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.

vinipace avatar Oct 01 '18 14:10 vinipace

What I'm getting now is a strange error

image

Adding @babel/plugin-proposal-export-default-from, as recommended, doesn't solve it.

vinipace avatar Oct 03 '18 14:10 vinipace

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?

raghavmac avatar Jun 04 '19 07:06 raghavmac

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.

balazsorban44 avatar Jan 31 '22 14:01 balazsorban44