question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using with Next.js

See original GitHub issue

Environment

"react": "17.0.2",
"react-native": "0.65.1",
"react-native-vector-icons": "^9.1.0",
"react-native-web": "^0.18.1",

Description

I’m trying to setup a NextJS app with React Native Web and React Native Vector Icons. I’ve got the RN-web part working properly, but I can’t get RN-vector-icons to work. As soon as a page includes a component coming from RN-vector-icon, I get the following error:

../app/node_modules/react-native-vector-icons/lib/create-icon-set.js
Module parse failed: Unexpected token (91:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
|       return (
>         <Text selectable={false} {...props}>
|           {glyph}
|           {children}

After some searching it appears I’m not the only one with the issue, yet I can’t seem to find a solution. On Next.js, this issue mentions non-transpired code and suggests using a plugin to fix the issue. However it does not resolve the issue. Here are a few links/resources I used to try and figure out what’s wrong in my setup:

Here’s my next.config.js including the custom webpack configuration. I am quite certain that this can be fixed by configuring webpack further but I can’t see how:

module.exports = withImages(withFonts({
  experimental: {
    externalDir: true
  },
  // This feature conflicts with next-images
  images: {
    disableStaticImages: true,
  },
  webpack: (config, options) => {
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      // Transform all direct `react-native` imports to `react-native-web`
      'react-native$': 'react-native-web',
      'react-native-linear-gradient': 'react-native-web-linear-gradient',
    }
    config.resolve.extensions = [
      '.web.js',
      '.web.ts',
      '.web.tsx',
      ...config.resolve.extensions,
    ]

    if (options.isServer) {
      config.externals = ['react', 'react-native-web', ...config.externals];
    }
    config.resolve.alias['react'] = path.resolve(__dirname, '.', 'node_modules', 'react');
    config.resolve.alias['react-native-web'] = path.resolve(__dirname, '.', 'node_modules', 'react-native-web');

    config.module.rules.push({
      test: /\.ttf$/,
      loader: "url-loader", // or directly file-loader
      include: [
        // Not sure which one should work so I added both possible paths
        path.resolve(__dirname, "..", "app", "node_modules", "react-native-vector-icons"), // as reported by the error, imported from monorepo shared code package
        path.resolve(__dirname, ".", "node_modules", "react-native-vector-icons"), // from this package
      ],
    })

    return config;
  }
}));

And here’s my Icon component:

import React from 'react'
import MaterialIcon from 'react-native-vector-icons/MaterialCommunityIcons'
import type { IconProps } from 'react-native-vector-icons/icon'

export const Icon = (props: IconProps) => (
  <MaterialIcon {...props} />
)

Note: when using the /dist version ('react-native-vector-icons/dist/MaterialCommunityIcons'), I’m getting another error: Error: Cannot find module 'react', so I believe it’s best to use it like so?

Do you guys have any idea/guidance as to how I can get react-native-vector-icons working with my Next.js setup?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
mohamedabadycommented, Aug 17, 2022

don’t forget to alias the import statement in package.json "alias": { ..., "react-native-vector-icons/MaterialCommunityIcons": "react-native-vector-icons/dist/MaterialCommunityIcons" }

1reaction
dennervidalcommented, Aug 4, 2022

I was able to resolve my issue. What I just did is import everything in react-native-vector-icons from dist folder. In my case, rollup wasn’t transpiling code from the lib root (esm), so the correct way is to use already bundled (cjs) code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started | Next.js
If you're new to Next.js, we recommend starting with the learn course. The interactive course with quizzes will guide you through everything you...
Read more >
Create a Next.js App | Learn Next.js
Next.js: The React Framework · An intuitive page-based routing system (with support for dynamic routes) · Pre-rendering, both static generation (SSG) and server- ......
Read more >
Setup - Create a Next.js App
You'll be using your own text editor and terminal app for this tutorial. ... the directory you'd like to create the app in,...
Read more >
Basic Features: Pages - Next.js
In Next.js, a page is a React Component exported from a .js , .jsx , .ts , or .tsx file in the pages...
Read more >
What is Next.js?
Next.js is a React framework that gives you building blocks to create web applications. By framework, we mean Next.js handles the tooling and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found