Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on types/packageInfo.js -- SyntaxError: Unexpected token 'export' #3374

Closed
ical10 opened this issue Apr 7, 2021 · 17 comments
Closed
Labels

Comments

@ical10
Copy link

ical10 commented Apr 7, 2021

Hello everyone, I am new to Polkadot.js so please bear with me.

I am making a Next.js app which tries to connect to a custom blockchain in one of my component (e.g. wallet.component.tsx), using the snippets below:

import { ApiPromise, WsProvider } from '@polkadot/api';

  const connectToBlockchain = async () => {
    const wsProvider = new WsProvider('https://address-to-rpc');
    const api = await ApiPromise.create({
      provider: wsProvider
      //types: types
    });
  };

However, when I execute my dev script: ts-node -r dotenv/config --project tsconfig.server.json server/index.ts

it returns an error:

My-project-root/node_modules/@polkadot/types/packageInfo.js:4
export const packageInfo = {
^^^^^^

SyntaxError: Unexpected token 'export'

The content of tsconfig.server.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "dist",
    "target": "es2017",
    "isolatedModules": false,
    "noEmit": false
  },
  "include": ["server/**/*.ts"]
}

and tsconfig.json:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
    "jsx": "preserve" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
    "declaration": true /* Generates corresponding '.d.ts' file. */,
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
    "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,

    "skipLibCheck": true,
    "baseUrl": ".",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "module": "esnext",
    "resolveJsonModule": true,
    "isolatedModules": true
  },
  "typeRoots": ["./node_modules/@polkadot/ts", "./node_modules/@types", "./src/types"],
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

server/index.ts only contains codes which 'converts' localhost protocol to https. Nothing fancy there.

Is this a problem with the package itself or a config error on my ts compiler? Please do let me know if I should address this issue to the next.js repo instead.

Your help is much appreciated, thank you!

@jacogr
Copy link
Member

jacogr commented Apr 7, 2021

Here is an SO question dealing with the same - https://stackoverflow.com/questions/65936222/next-js-syntaxerror-unexpected-token-export

Above (and also on the next.js repo), other devs keep on pointing to https://github.com/martpie/next-transpile-modules#readme as a solution for using ESM dependencies

@ical10
Copy link
Author

ical10 commented Apr 7, 2021

Thanks for the rapid response, @jacogr . I have installed the module for transpiling, but then another error occurred:

event - build page: /home
wait  - compiling...
event - compiled successfully
TypeError: Class extends value undefined is not a constructor or null
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js:14:81)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1778:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js:13:73)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1802:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js:5:79)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1790:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/index.js:3:70)
TypeError: Class extends value undefined is not a constructor or null
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js:14:81)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1778:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js:13:73)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1802:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js:5:79)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1790:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/index.js:3:70)
TypeError: Class extends value undefined is not a constructor or null
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js:14:81)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1778:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js:13:73)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1802:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js:5:79)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1790:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/index.js:3:70)
TypeError: Class extends value undefined is not a constructor or null
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js:14:81)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MagicNumber.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1778:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js:13:73)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/MetadataVersioned.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1802:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js:5:79)
    at Module../node_modules/@polkadot/api/node_modules/@polkadot/metadata/Metadata.js (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:1790:1)
    at __webpack_require__ (/Users/rizal/GDrive/blocksphere/myri/myriad/.next/server/pages/home.js:23:31)
    at eval (webpack-internal:///./node_modules/@polkadot/api/node_modules/@polkadot/metadata/index.js:3:70)

Inside next.config.js:

const withTM = require('next-transpile-modules')(['@polkadot/api', '@polkadot/types']);
module.exports = withTM({
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      issuer: {
        test: /\.(js|ts)x?$/
      },
      use: ['@svgr/webpack']
    });

    return config;
  }
});

Any clue?

@jacogr
Copy link
Member

jacogr commented Apr 7, 2021

In the above, it seems now that since @polkadot/metadata is not in there, it has issues importing. Wondering if @polkadot/ itself will just pickup all the scoped packages, since otherwise would need to supply all of them.

@ical10
Copy link
Author

ical10 commented Apr 7, 2021

supplying @polkadot/ (or with globbing/wildcard) throws an error:

/Users/rizal/GDrive/blocksphere/myri/myriad/node_modules/next-transpile-modules/src/next-transpile-modules.js:136
        throw new Error(
              ^
Error: next-transpile-modules - an unexpected error happened when trying to resolve "@polkadot/"

If I need to supply all of them, how many are they?

@jacogr
Copy link
Member

jacogr commented Apr 7, 2021

Ummm, that is a horrible solution then.

It would depend on what is actually used, so ls -al node_modules/@polkadot would have the list as installed.

@ical10
Copy link
Author

ical10 commented Apr 8, 2021

This is the output of ls -al node_modules/@polkadot

total 0
drwxr-xr-x   25 user  staff    800  7 Apr 15:20 api
drwxr-xr-x   37 user staff   1184  7 Apr 15:20 api-derive
drwxr-xr-x   16 user staff    512  7 Apr 15:25 extension-dapp
drwxr-xr-x   17 user  staff    544  7 Apr 15:20 hw-ledger
drwxr-xr-x   20 user  staff    640  7 Apr 15:20 hw-ledger-transports
drwxr-xr-x   26 user  staff    832  7 Apr 15:20 keyring
drwxr-xr-x    8 user staff    256  7 Apr 15:20 metadata
drwxr-xr-x   14 user  staff    448  7 Apr 15:20 networks
drwxr-xr-x   20 user  staff    640  7 Apr 15:33 rpc-core
drwxr-xr-x   16 user  staff    512  7 Apr 15:20 rpc-provider
drwxr-xr-x   35 user  staff   1120  7 Apr 15:20 ts
drwxr-xr-x   25 user  staff    800  7 Apr 16:29 types
drwxr-xr-x   19 user  staff    608  7 Apr 15:20 types-known
drwxr-xr-x   30 user  staff    960  7 Apr 15:20 ui-keyring
drwxr-xr-x   18 user  staff    576  7 Apr 15:20 ui-settings
drwxr-xr-x   43 user  staff   1376  7 Apr 15:20 util
drwxr-xr-x   30 user  staff    960  7 Apr 15:20 util-crypto
drwxr-xr-x   15 user staff    480  7 Apr 15:20 wasm-crypto
drwxr-xr-x    8 user  staff    256  7 Apr 15:20 x-fetch
drwxr-xr-x   10 user  staff    320  7 Apr 15:20 x-global
drwxr-xr-x   15 user  staff    480  7 Apr 15:20 x-rxjs
drwxr-xr-x    8 user  staff    256  7 Apr 15:20 x-ws

It is awful if I need to include all of them. Isn't there any workaround? Thank you again for assisting me.

@jacogr
Copy link
Member

jacogr commented Apr 8, 2021

All the information for compilation is included in the package.json for the bundler -

  • an exports map is provided so based on require/import usage the bundler can use the cjs or esm version
  • the Package is indicates as an esm package (as default), by the type: module identifier in the package.json

So really not sure why next ignores all this info. (Especially since there is a drive for packages to publish as esm now that Node is fully on-board)

The only option I see in this case is to specify each package with the Plugin (which is terrible since they are granular). If it was me I would do an fs.readDir and dynamically populate the list. (Still horrible, but at least dynamic)

Will need to read more about next and their plans in their issue tracker, cannot see that having all esm modules done via a plugin is a long-term solution, at some point basically all will need to go through that route.

@ical10
Copy link
Author

ical10 commented Apr 9, 2021

okay, so I have specified each package with the Plugin and the error persists.

By doing ls node_modules/@polkadot/hw-ledger-transports/, which outputs:

LICENSE                 browser.js              package.json            react-native.cjs        types.d.ts
README.md               node.cjs                packageInfo.cjs         react-native.d.ts       types.js
browser.cjs             node.d.ts               packageInfo.d.ts        react-native.js
browser.d.ts            node.js                 packageInfo.js          types.cjs

I realise that I have mistakenly modified the package.json by adding these lines inside the exports field:

      "require": "./index.cjs",
      "default": "./index.js",

which is dumb because such files don't exist. Changing it into:

      "require": "./node.cjs",
      "default": "./node.js",

fix the problem.

However, when the transpile module 'reads' other packages which does not contain exports field in their package.json (in my case they are @polkadot/metadata and @polkadot/ts), again it throws an error.

Error: next-transpile-modules - an unexpected error happened when trying to resolve "@polkadot/metadata"
Error: Can't resolve '@polkadot/metadata' in '/Users/rizal/GDrive/blocksphere/myri/myriad'
    at getPackageRootDirectory (/Users/rizal/GDrive/blocksphere/myri/myriad/node_modules/next-transpile-modules/src/next-transpile-modules.js:136:15)
    at Array.map (<anonymous>)
    at withTM (/Users/rizal/GDrive/blocksphere/myri/myriad/node_modules/next-transpile-modules/src/next-transpile-modules.js:145:34)
    at Object.<anonymous> (/Users/rizal/GDrive/blocksphere/myri/myriad/next.config.js:26:18)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Module.m._compile (/Users/rizal/GDrive/blocksphere/myri/myriad/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Object.require.extensions.<computed> [as .js] (/Users/rizal/GDrive/blocksphere/myri/myriad/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)

Ignoring these packages will bring me back to the same error where metadata is not recognised
( i.e. TypeError: Class extends value undefined is not a constructor or null)

Since the metadata can not be read by the transpile-module, I think I am revisiting the earlier issue and now I am more clueless as to where to start debugging this.

@jacogr
Copy link
Member

jacogr commented Apr 9, 2021

The metadata package does have the export map. The build scripts are the same.

Is there possibly a version mismatch, ie. the metadata not being the same version as the api and types?

@ical10
Copy link
Author

ical10 commented Apr 9, 2021

You're right.

metadata and types version is 1.34.1, while api version is 4.4.1, I completely missed that.

I have upgraded both to match api version, but the error still persists.

@jacogr
Copy link
Member

jacogr commented Apr 9, 2021

That is weird, the API types/dependencies only refer to the current version of the metadata. For instance for 4.4.1 -

https://github.com/polkadot-js/api/blob/v4.4.1/packages/api/package.json#L18-L31

So if you have them specified explicitly in your package.json, just bump the version there. Alternatively if you only have the API installed (don't see how the dependency should go wrong since the versions are specified in package.json for the same versions), cleaning node_modules and re-installing should do the trick.

@ical10
Copy link
Author

ical10 commented Apr 9, 2021

That's it!

After making sure that there's no duplication of polkadot packages within the devDependencies and dependencies fields in package.json, cleaning node_modules and re-installing solved it!

However, now after each page compilation I got this error:

event - build page: /
wait  - compiling...
event - compiled successfully
Multiple instances of @polkadot/wasm-crypto detected, ensure that there is only one package in your dependency tree.
        4.0.2   <unknown>
        4.0.2   <unknown>
        4.0.2   <unknown>
        4.0.2   <unknown>
Multiple instances of @polkadot/util-crypto detected, ensure that there is only one package in your dependency tree.
        6.0.5   <unknown>
        6.0.5   <unknown>
        6.0.5   <unknown>
        6.0.5   <unknown>
Multiple instances of @polkadot/keyring detected, ensure that there is only one package in your dependency tree.
        2.18.1  <unknown>
        2.18.1  <unknown>
        2.18.1  <unknown>

The modules being referred differ depending on which page being compiled, but it is always @polkadot/

Was there something that I miss?

@ical10
Copy link
Author

ical10 commented Apr 9, 2021

Right, so now I wanted to instantiate the API inside one of my React component using this snippet:

const wsProvider = new WsProvider('wss://rpc.myriad.systems/');
  94 | console.log('wsProvider:', wsProvider);
> 95 | const api = await ApiPromise.create({
     |                             ^
  96 |   provider: wsProvider,
  97 |   types: types
  98 | });

but then at line 95 the code breaks and the console logs this error:

Uncaught (in promise) TypeError: Object(...) is not a function

wsProvider status is connected when I console.log it.

@jacogr
Copy link
Member

jacogr commented Apr 12, 2021

Multiple instances of @polkadot/wasm-crypto detectedindeed means exactly what it says - there are multiple packages being instantiated. So in the case above, thekeyringlooks like the culprit since it is not the same version asutil-crypto`. Basically it would mean that the node modules looks like the follows -

util-crypto/node_modules/@polkadot/...
keyring/node_modules/@polkadot/...

Generally npm find-dupes will show the duplicates (if any)

@ical10
Copy link
Author

ical10 commented Apr 16, 2021

Okay, thanks for keeping up with me, you're the best!

The Multiple instances warning does not really mess up the app, so I'll leave it for now.

I was finally able to find a fix to this, following these steps:

  1. In package.json, make sure that resolutions field does not contain the package
  2. Include only @polkadot/api first in dependencies
  3. Update to the latest version of polkadot API package
  4. Add the following lines in the webpack config inside next.config.js
    config.module.rules.push({
      test: /\.mjs$/,
      include: /node_modules/,
      type: 'javascript/auto'
    });

The fourth step had already been discussed before but I can not find the link to the thread.

Run with yarn dev and it should work normally.

@jacogr
Copy link
Member

jacogr commented May 29, 2021

This issue has been open for 21 days with no activity and is not labelled as an enhancement. It will be closed in 7 days.

@polkadot-js-bot
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jun 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants