小编brc*_*-dd的帖子

如何在 next.js 中加载导出 Typescript 的库

当尝试从导出Typescript 的私有库导入组件时,我们收到以下错误消息:

Module parse failed: Unexpected token (82:7)
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
| 
| // Types
> export type {
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我尝试在tsconfig文件中显式包含库节点模块:

  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "node_modules/@private-lib/*.*"
  ],
  "exclude": [""]
Run Code Online (Sandbox Code Playgroud)

但不幸的是,没有效果。似乎可以更改next.jswebpack配置,但不幸的是,尝试插入 Typescript 加载器并不起作用:

module.exports = {
  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.(ts|js)x?$/,
      use: [
        options.defaultLoaders.babel,
        {
          loader: "ts-loader",
          options: {
            transpileOnly: true,
            experimentalWatchApi: …
Run Code Online (Sandbox Code Playgroud)

typescript webpack next.js webpack-4

21
推荐指数
3
解决办法
2万
查看次数

Next js 生产构建是否需要服务器中的 node_modules 文件夹?

我对 Next js 及其部署过程相当陌生。我最近将我的一个 React js 项目转换为 Next js,以便利用 Next js 提供的服务器端渲染功能。现在到了部署时间,我发现如果node_modules服务器中没有该文件夹,下一个版本将无法部署。我在我的页面中使用getServerSideProps并使用"build": "next build"package.json 中的命令进行构建。问题是我的 node_modules 文件夹接近 300MB(.next构建又增加了大约 10MB),我认为每次部署都伴随这么大的重量并不是最佳实践(我打算部署此构建的不同实例,因此 310MB)服务器中的 X 个实例)。我在这里做错了什么还是这是实现这一目标的唯一方法?我很感激任何答案。谢谢...

node.js reactjs server-side-rendering next.js

17
推荐指数
2
解决办法
2万
查看次数

反冲:重复的原子键 - 在 nextjs 中

我确实在我的 nextjs 应用程序中使用了反冲力。但是,如果我接下来运行(在开发或生产中没有区别),我会收到以下错误消息:

重复的原子键“companyData”。这是生产中的致命错误。但如果由于热模块更换而发生此警告,则可以安全地忽略此警告。

这是我实现它的方式:

/src/stores/CompanyStore.js

import { useSetRecoilState, useRecoilValue , atom } from 'recoil';
import config from '../../content/config.yml';

const companyData = atom({
  key: 'companyData',
  default: {...config.company},
});

export const useSetCompanyData = () => useSetRecoilState(companyData);
export const useCompanyData = () => useRecoilValue(companyData);


export default {
  useSetCompanyData,
  useCompanyData,
};
Run Code Online (Sandbox Code Playgroud)

我在某些组件中这样使用它:

我的组件.js

import React from 'react';
...
...

import {useCompanyData} from '../stores/CompanyStore';

const MyComponent = () => {
  const classes = useStyles();
  const companyData = useCompanyData();
  const { summary: headline, description …
Run Code Online (Sandbox Code Playgroud)

reactjs next.js recoiljs

14
推荐指数
2
解决办法
2万
查看次数

如何覆盖 Next.js `*.svg` 模块声明?

Next.js 最近进行了修改(在 v11.0.x 中),其类型定义如下:

next-env.d.ts(不可修改,在每次构建时重新生成):

/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />
Run Code Online (Sandbox Code Playgroud)

node_modules/next/image-types/global.d.ts(不可修改,不想使用patch-package)中:

declare module '*.svg' {
  const content: any
  export default content
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是我正在使用@svgr/webpack,因此我需要执行以下操作:

declare module '*.svg' {
  const content: React.FC<React.SVGAttributes<SVGElement>>
  export default content
}
Run Code Online (Sandbox Code Playgroud)

之前将此代码放置在index.d.ts用于工作的资产文件夹中。但现在不行了,结果我被迫单独投射每个导入。有什么办法可以直接做到这一点吗?

typescript reactjs next.js nextjs-image

10
推荐指数
2
解决办法
5306
查看次数

Next.js - 导出index.html

我想在Amazon S3上托管我的 React 项目。

我正在开发它Next.js

文件夹树如下所示。

pages
 |- auth
 |    |- index.tsx
 |- (...)
 |- index.tsx
Run Code Online (Sandbox Code Playgroud)

我做到了

next build && next export
Run Code Online (Sandbox Code Playgroud)

构建和导出后,我期望它

out
 |- _next
 |- auth
 |    |- index.html /* I want another index.html */
 |- (...)
 |- index.html
 |- static
Run Code Online (Sandbox Code Playgroud)

但我明白了,

 |- _next
 |- auth.html /*I need /auth/index.html*/
 |- (...)
 |- index.html
 |- static
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现呢。

先感谢您。

next.js

9
推荐指数
2
解决办法
2万
查看次数

nextjs - 使用 NODE_ENV=development 进行下一个构建

我想将我的nextjs项目构建为开发模式。

我试过喜欢它

包.json

{
  ...
  "scripts": {
    "dev": "next",
    "build:dev": "set NODE_ENV=development & next build",
    "build:prod": "set NODE_ENV=production & next build",
    "start:dev": "set NODE_ENV=development & next start",
    "start:prod": "set NODE_ENV=production & next start"
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

下一个.config.js

module.exports = withSass({
  env: {
    baseUrl: process.env.NODE_ENV === "development" ? "devServerURL": "prodServerURL"
  }
});
Run Code Online (Sandbox Code Playgroud)

但我无法实现我想要的。

所以,我尝试了一些改变。

包.json

  "scripts": {
    "dev": "next",
    "build": "next build",
    "start:dev": "set NODE_ENV=development & next start",
    "start:prod": "set NODE_ENV=production & next start"
  }
Run Code Online (Sandbox Code Playgroud)

但它也不起作用。

如何构建 …

reactjs next.js

9
推荐指数
2
解决办法
5745
查看次数

Next.js - 纱线开发:错误命令失败,信号为“SIGSEGV”

每当我跑步yarn devnpm run dev

yarn run v1.22.10
warning ../../../../package.json: No license field
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: no next.config.js https://nextjs.org/docs/messages/webpack5
event - compiled successfully
event - build page: /
wait  - compiling...
error Command failed with signal "SIGSEGV".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Run Code Online (Sandbox Code Playgroud)

package.json

yarn run v1.22.10
warning ../../../../package.json: No license field
$ next dev
ready - started server on …
Run Code Online (Sandbox Code Playgroud)

npm package.json yarnpkg next.js

8
推荐指数
1
解决办法
1万
查看次数

Discord.js 嵌入宽度不可靠

我正在使用 Discord.js 库创建一个 Discord 机器人。每当我向文本通道发送嵌入消息时,其宽度会随着不同的数据而不断变化。

const celestialObject = new MessageEmbed()
            .setColor("#F0386B")
            .setTitle(
              res.data.name == res.data.englishName
                ? res.data.englishName
                : `${res.data.englishName} (${res.data.name})`
            )
            .attachFiles(attachment)
            .setThumbnail("attachment://logo.png")

            .addFields(
              {
                name: "```Density```",
                value: res.data.density.toFixed(2) + " g/cm^3",
                inline: true,
              },
              {
                name: "```Gravity```",
                value: res.data.gravity + " m/s^2",
                inline: true,
              },
              {
                name: "```Moons```",
                value: res.data.moons ? Object.keys(res.data.moons).length : 0,
                inline: true,
              },
              {
                name: "```Mass```",
                value: `
                    ${res.data.mass.massValue.toFixed(2)}^
                    ${res.data.mass.massExponent} kgs
`,
                inline: true,
              },
              {
                name: "```Escape Velocity```",
                value: (res.data.escape / 1000).toFixed(1) + " …
Run Code Online (Sandbox Code Playgroud)

javascript api discord discord.js

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

如何停止随机节点错误:“(节点:196569)警告:套接字上已发出错误事件。”

我有一个用 Node.js 构建的 Express Web 服务器。当我运行服务器时,最终我在终端中收到以下错误:

(node:196569) Warning: An error event has already been emitted on the socket. Please use the destroy method on the socket while handling a 'clientError' event.
(Use `node --trace-warnings ...` to show where the warning was created)
Run Code Online (Sandbox Code Playgroud)

我正在使用 node:https 创建服务器,如下所示:

const express = require('express');
const https = require('node:https');

// setting up express
const app = express();

// Setting up SSL Certificates
const https_options = {
    key: process.env.HUTCHYBOP_KEY.replace(/\\n/g, '\n'),
    cert: process.env.CRT_CODE.replace(/\\n/g, '\n'),
    keepAlive: false
};

... …
Run Code Online (Sandbox Code Playgroud)

https node.js express

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

Next JS config 多插件配置

const {
  DEVELOPMENT_SERVER,
  PRODUCTION_BUILD
} = require("next/constants");

require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

const nextConfig = {
  webpack: config => ({ ...config, node: { fs: "empty" } })
};
module.exports = phase => {
  if (phase === DEVELOPMENT_SERVER || phase === PRODUCTION_BUILD) {
    const withCSS = require("@zeit/next-css");
    return withCSS(nextConfig);
  }
  return nextConfig;
};
*module.exports =  {
  webpack: (config) => {
    config.plugins = config.plugins || []
    config.plugins = [
      ...config.plugins,
      // Read the .env file
      new Dotenv({
        path: …
Run Code Online (Sandbox Code Playgroud)

apollo reactjs webpack next.js

5
推荐指数
2
解决办法
4629
查看次数