npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guaclive/next-transpile-modules

v3.3.4

Published

Next.js plugin to transpile code from node_modules (supports TypeScript)

Downloads

3

Readme

Next.js + Transpile node_modules

Build Status Dependencies

Transpile untranspiled modules from node_modules using the Next.js Babel configuration. Makes it easy to have local libraries and keep a slick, manageable dev experience.

Supports all extensions supported by Next.js: .js, .jsx, .ts, .tsx, .mjs, .css, .scss and .sass.

What problems does it solve?

This plugin aims to solve the following challenges:

  • code transpilation from local packages (think: a monorepo with a styleguide package)
  • code transpilation from NPM modules using ES6 imports (e.g lodash-es)

What this plugin does not aim to solve:

  • any-package IE11-compatible maker

Compatibility table

| Next.js version | Plugin version | | --------------- | -------------- | | Next.js 9.2+ | 3.x | | Next.js 8 / 9 | 2.x | | Next.js 6 / 7 | 1.x |

Installation

npm install --save next-transpile-modules

or

yarn add next-transpile-modules

Usage

Classic:

// next.config.js
const withTM = require('next-transpile-modules')(['somemodule', 'and-another']); // pass the modules you would like to see transpiled

module.exports = withTM();

note: please declare withTM as your last plugin (the "most nested" one).

Scoped packages

You can include scoped packages or nested ones:

const withTM = require('next-transpile-modules')(['@shared/ui', '@shared/utils']);

// ...
const withTM = require('next-transpile-modules')(['styleguide/components']);

// ...

With next-compose-plugins:

const withPlugins = require('next-compose-plugins');
const withTM = require('next-transpile-modules')(['some-module', 'and-another']);

module.exports = withPlugins([withTM], {
  // ...
});

CSS/SCSS support

Since next-transpile-modules@3 and next@>9.2, this plugin can also transpile CSS included in your transpiled packages. SCSS/SASS is also supported since [email protected].

In your transpiled package:

// shared-ui/components/Button.js
import styles from './Button.module.css';

function Button(props) {
  return (
    <button type='button' className={styles.error}>
      {props.children}
    </button>
  );
}

export default Button;
/* shared-ui/components/Button.module.js */
.error {
  color: white;
  background-color: red;
}

In your app:

// next.config.js
const withTM = require('next-transpile-modules')(['shared-ui']);

// ...
// pages/home.jsx
import React from 'react';
import Button from 'shared-ui/components/Button';

const HomePage = () => {
  return (
    <main>
      {/* will output <button class="Button_error__xxxxx"> */}
      <Button>Styled button</Button>
    </main>
  );
};

export default HomePage;

It also supports global CSS import packages located in node_modules:

// pages/_app.js
import 'shared-ui/styles/global.css'; // will be imported globally

export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

FAQ

What is the difference with @weco/next-plugin-transpile-modules?

  • it is maintained, @weco's seems dead
  • it supports TypeScript
  • it supports CSS modules (since Next.js 9.2)
  • it supports .mjs

I have trouble making it work with Next.js 7

Next.js 7 introduced Webpack 4 and Babel 7, which changed a couple of things, especially for TypeScript and Flow plugins.

If you have a transpilation error when loading a page, check that your babel.config.js is up to date and valid, you may have forgotten a preset there.

I have trouble with transpilation and Flow/TypeScript

In your Next.js app, make sure you use a babel.config.js and not a .babelrc as Babel's configuration file (see explanation below).

Since Next.js 9, you probably don't need that file anymore, as TypeScript is supported natively.

I have trouble with transpilation and Yarn workspaces

If you get a transpilation error when using Yarn workspaces, make sure you are using a babel.config.js and not a .babelrc. The former is a project-wide Babel configuration, when the latter works for relative paths only (and won't work as Yarn install dependencies in a parent directory).

I have trouble with transpilation and my custom .babelrc

Make sure you are using a babel.config.js and not a .babelrc. The former is a project-wide Babel configuration, when the latter works for relative paths only.

I have trouble with Yarn and hot reloading

If you add a local library (let's say with yarn add ../some-shared-module), Yarn will copy those files by default, instead of symlinking them. So your changes to the initial folder won't be copied to your Next.js node_modules directory.

You can go back to npm, or use Yarn workspaces. See an example in the official Next.js repo.

I have trouble making it work with Lerna

Lerna's purpose is to publish different packages from a monorepo, it does not help for and does not intend to help local development with local modules (<- this, in caps).

This is not coming from me, but from Lerna's maintainer.

So you are probably using it wrong, and I advice you to use npm or Yarn workspaces instead.

But... I really need to make it work with Lerna!

You may need to tell your Webpack configuration how to properly resolve your scoped packages, as they won't be installed in your Next.js directory, but the root of your Lerna setup.

const withTM = require('next-transpile-modules')(['@your-project/shared', '@your-project/styleguide']);

module.exports = withTM({
  webpack: (config, options) => {
    config.resolve.alias = {
      ...config.resolve.alias,
      // Will make webpack look for these modules in parent directories
      '@your-project/shared': require.resolve('@your-project/shared'),
      '@your-project/styleguide': require.resolve('@your-project/styleguide')
      // ...
    };
    return config;
  }
});

I have trouble with duplicated dependencies or the Invalid hook call error in react

It can happen that when using next-transpile-modules with a local package and npm, you end up with duplicated dependencies in your final Next.js build. It is important to understand why it happens.

Let's take the following setup: one Next.js app ("Consumer"), and one Styleguide library.

You will probably have react as a peerDependencies and as a devDependecy of the Styleguide. If you use npm i, it will create a symlink to your Styleguide package in your "Consumer" node_modules.

The thing is in this shared package, you also have a node_modules. So when your shared modules requires, let's say react, Webpack will resolve it to the version in your Styleguide's node_modules, and not your Consumer's node_modules. Hence the duplicated react in your final bundles.

You can tell Webpack how to resolve the react of your Styleguide to use the version in your Next.js app like that:

const withTM = require('next-transpile-modules')(['styleguide']);

module.exports = withTM({
  webpack: (config) => {
+   config.resolve.alias['react'] = path.resolve(__dirname, '.', 'node_modules', 'react');

    return config
  },
});

Please note, the above will only work if react is properly declared as peerDependencies or devDependencies in your referenced package.

It is not a great solution, but it works. Any help to find a more future-proof solution is welcome.