UNPKG

3.69 kBMarkdownView Raw
1# Next.js + Transpile `node_modules`
2
3Transpile untranspiled modules from `node_modules`.
4Makes it easy to have local libraries and keep a slick, manageable dev experience.
5
6## Compatibility table
7
8| Next.js version | Plugin version |
9|-----------------|----------------|
10| Next.js 8 | 2.x |
11| Next.js 6 / 7 | 1.x |
12
13## Installation
14
15```
16npm install --save next-transpile-modules
17```
18
19or
20
21```
22yarn add next-transpile-modules
23```
24
25## Usage
26
27Classic:
28
29```js
30// next.config.js
31const withTM = require('next-transpile-modules');
32
33module.exports = withTM({
34 transpileModules: ['somemodule', 'and-another']
35});
36```
37
38**note:** please declare `withTM` as your last plugin (the "most nested" one).
39
40Example with `next-typescript`:
41
42```js
43const withTypescript = require('@zeit/next-typescript');
44const withTM = require('next-transpile-modules');
45
46module.exports = withTypescript(
47 withTM({
48 transpileModules: ['somemodule', 'and-another']
49 })
50);
51```
52
53With `next-compose-plugins`:
54
55```js
56const withPlugins = require('next-compose-plugins');
57
58const withTypescript = require('@zeit/next-typescript');
59const withTM = require('next-transpile-modules');
60
61module.exports = withPlugins([
62 [withTM, {
63 transpileModules: ['some-module', 'and-another'],
64 }],
65 withTypescript,
66], {
67 // ...
68});
69```
70
71## FAQ
72
73### What is the difference with `@weco/next-plugin-transpile-modules`?
74
75- it is maintained, `@weco`'s seems dead
76- it supports TypeScript
77
78### I have trouble making it work with Next.js 7
79
80Next.js 7 introduced Webpack 4 and Babel 7, [which changed a couple of things](https://github.com/zeit/next.js/issues/5393#issuecomment-458517433), especially for TypeScript and Flow plugins.
81
82If you have a transpilation error when loading a page, check that your `.babelrc`/`babel.config.js` is up to date and valid, [you may have forgotten a preset](https://github.com/martpie/next-transpile-modules/issues/1#issuecomment-427749256) there.
83
84### I have trouble with Yarn and hot reloading
85
86If 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.
87
88You can go back to `npm`, or use Yarn workspaces. See [an example](https://github.com/zeit/next.js/tree/canary/examples/with-yarn-workspaces) in the official Next.js repo.
89
90### I have trouble making it work with Lerna
91
92Lerna'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**.
93
94This is not coming from me, but [from Lerna's maintainer](https://github.com/lerna/lerna/issues/1243#issuecomment-401396850).
95
96So you are probably [using it wrong](https://github.com/martpie/next-transpile-modules/issues/5#issuecomment-441501107), and I advice you to use `npm` or Yarn workspaces instead.
97
98### But... I really need to make it work with Lerna!
99
100You 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.
101
102```js
103const withTM = require('next-transpile-modules');
104
105module.exports = withTM({
106 transpileModules: ['@your-project/shared', '@your-project/styleguide'],
107 webpack: (config, options) => {
108 config.resolve.alias = {
109 ...config.resolve.alias,
110 // Will make webpack look for these modules in parent directories
111 '@your-project/shared': require.resolve('@your-project/shared'),
112 '@your-project/styleguide': require.resolve('@your-project/styleguide'),
113 // ...
114 };
115 return config;
116 },
117});
118```