小编Faw*_*lil的帖子

在 Next.js 中转换 npm 模块

我正在尝试从 node_modules 转译一个模块“react-device-detect”,但还无法实现。以下是我尝试过的:

module.exports = withLess({
  webpack: (config, { isServer, defaultLoaders }) => {
    // other configs
    config.module.rules.push({
      test: /\\.+(ts|tsx)$/,
      include: [path.join(__dirname, 'node_modules/react-device-detect')],
      // exclude: /node_modules/,
      use: [{ loader: 'ts-loader' }],
    });

    config.module.rules.push({
      test: /\\.+(js|jsx)$/,
      include: [path.join(__dirname, 'node_modules/react-device-detect')],
      // exclude: /node_modules/,
      use: [{ loader: 'babel-loader' }],
    });
    return config;
  },
});
Run Code Online (Sandbox Code Playgroud)

我也单独尝试过这些规则,但没有奏效。

更新 1: 完成 next.config.js 配置 @felixmosh

const webpack = require("webpack");
const withLess = require("@zeit/next-less");
const { parsed: localEnv } = require("dotenv").config();
require("dotenv").config({
  path: process.env.NODE_ENV === "production" ? ".env.production" …
Run Code Online (Sandbox Code Playgroud)

node-modules webpack next.js

6
推荐指数
1
解决办法
6751
查看次数

标签 统计

next.js ×1

node-modules ×1

webpack ×1