NextJS 14(带电子)尝试使用 .node 插件 - 找不到模块

问题描述 投票:0回答:1

我创建了一个版本为 14.2.2 的 next.js 应用程序 我使用以下教程构建了一个nodejs插件: https://morioh.com/a/e1a6f79af449/how-to-write-nodejs-addons-using-c-and-n-api-for-beginners 现在我确实有一个我想使用的 my-addon.node 文件。

我有以下结构:

my-nextjs-app/
├── lib/
│   └── my-addon.node
├── pages/
│   ├── api/
│   │   └── hello/
|   |        └── route.ts
├── next.config.mjs
├── package.json

route.ts:

import { NextResponse } from "next/server";


import path from 'path';
const addonPath = path.join(process.cwd(), 'lib', 'addon.node');
const myAddon = require(addonPath);

 
export async function GET() {
  try {
    const result = myAddon.hello();
    return NextResponse.json({
      message: 'test'
    });
  } catch(error) {
    return NextResponse.json({
      message: error.message
    });
  }
  
}

next.config.mjs:

/** @type {import('next').NextConfig} */

const nextConfig = {
    webpack: (config, { isServer }) => {
      if (isServer) {
        config.externals = config.externals || [];
        config.externals.push((context, request, callback) => {
          if (/\.node$/.test(request)) {
            callback(null, 'commonjs ' + request);
          } else {
            callback();
          }
        });
      }
  
      return config;
    },
  };

  export default nextConfig;

package.json

{
  "name": "my-first-nextjs-app",
  "version": "0.1.0",
  "private": true,
  "main": "main.js",
  "author": "Mart",
  "description": "-",
  "scripts": {
    "dev": "concurrently -n \"NEXT,ELECTRON\" -c \"yellow,blue\" --kill-others \"next dev\" \"electron .\"",
    "build": "next build && electron-builder",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "electron-serve": "^1.3.0",
    "next": "14.2.2",
    "react": "^18",
    "react-dom": "^18",
    "swig":"^1.4.2"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "concurrently": "^8.2.2",
    "electron": "^30.0.1",
    "electron-builder": "^24.13.3",
    "eslint": "^8",
    "eslint-config-next": "14.2.2",
    "postcss": "^8",
    "tailwindcss": "^3.4.1",
    "typescript": "^5"
  },
  "license": "ISC"
}

现在,我得到“MODULE_NOT_FOUND”。

完整错误日志:

 ○ Compiling /api/hello ...
[NEXT] (node:32096) [DEP_WEBPACK_EXTERNALS_FUNCTION_PARAMETERS] DeprecationWarning: The externals-function should be defined like ({context, request}, cb) => { ... }
[NEXT] (Use `node --trace-deprecation ...` to show where the warning was created)
[NEXT]  ⚠ ./src/app/api/hello/route.ts
[NEXT] Critical dependency: the request of a dependency is an expression
[NEXT]
[NEXT] Import trace for requested module:
[NEXT] ./src/app/api/hello/route.ts
[NEXT]  ⚠ Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
[NEXT]  ⨯ Error: Cannot find module 'C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\src\lib\addon.node'
[NEXT]     at webpackEmptyContext (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:22:10)
[NEXT]     at eval (webpack-internal:///(rsc)/./src/app/api/hello/route.ts:17:80)
[NEXT]     at (rsc)/./src/app/api/hello/route.ts (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:95:1)
[NEXT]     at __webpack_require__ (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\webpack-runtime.js:33:42)
[NEXT]     at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fhello%2Froute&page=%2Fapi%2Fhello%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fhello%2Froute.ts&appDir=C%3A%5CUsers%5CUser%5CDocuments%5CProjecten%5CNextJSIntro%5Cmy-first-nextjs-app%5Csrc%5Capp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=C%3A%5CUsers%5CUser%5CDocuments%5CProjecten%5CNextJSIntro%5Cmy-first-nextjs-app&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:15:159)
[NEXT]     at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fhello%2Froute&page=%2Fapi%2Fhello%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fhello%2Froute.ts&appDir=C%3A%5CUsers%5CUser%5CDocuments%5CProjecten%5CNextJSIntro%5Cmy-first-nextjs-app%5Csrc%5Capp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=C%3A%5CUsers%5CUser%5CDocuments%5CProjecten%5CNextJSIntro%5Cmy-first-nextjs-app&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:84:1)
[NEXT]     at __webpack_require__ (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\webpack-runtime.js:33:42)
[NEXT]     at __webpack_exec__ (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:105:39)
[NEXT]     at C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:106:83
[NEXT]     at __webpack_require__.X (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\webpack-runtime.js:168:21)
[NEXT]     at C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:106:47
[NEXT]     at Object.<anonymous> (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\.next\server\app\api\hello\route.js:109:3)
[NEXT]     at Module._compile (node:internal/modules/cjs/loader:1369:14)
[NEXT]     at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
[NEXT]     at Module.load (node:internal/modules/cjs/loader:1206:32)
[NEXT]     at Module._load (node:internal/modules/cjs/loader:1022:12)
[NEXT]     at Module.require (node:internal/modules/cjs/loader:1231:19)
[NEXT]     at mod.require (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\require-hook.js:65:28)
[NEXT]     at require (node:internal/modules/helpers:179:18)
[NEXT]     at requirePage (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\require.js:109:84)
[NEXT]     at C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\load-components.js:98:84
[NEXT]     at async loadComponentsImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\load-components.js:98:26)
[NEXT]     at async DevServer.findPageComponentsImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\next-server.js:710:36)
[NEXT]     at async DevServer.findPageComponents (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\dev\next-dev-server.js:577:20)
[NEXT]     at async DevServer.renderPageComponent (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\base-server.js:1910:24)
[NEXT]     at async DevServer.renderToResponseImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\base-server.js:1962:32)
[NEXT]     at async DevServer.pipeImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\base-server.js:920:25)
[NEXT]     at async NextNodeServer.handleCatchallRenderRequest (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\next-server.js:272:17)
[NEXT]     at async DevServer.handleRequestImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\base-server.js:816:17)
[NEXT]     at async C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\dev\next-dev-server.js:339:20
[NEXT]     at async Span.traceAsyncFn (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\trace\trace.js:154:20)
[NEXT]     at async DevServer.handleRequest (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\dev\next-dev-server.js:336:24)
[NEXT]     at async invokeRender (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\lib\router-server.js:174:21)
[NEXT]     at async handleRequest (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\lib\router-server.js:353:24)
[NEXT]     at async requestHandlerImpl (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\lib\router-server.js:377:13)
[NEXT]     at async Server.requestListener (C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\node_modules\next\dist\server\lib\start-server.js:141:13) {
[NEXT]   code: 'MODULE_NOT_FOUND',
[NEXT]   page: '/api/hello'
[NEXT] }

对我可能会错过的东西有什么想法吗? 我是 next.js 的新手,所以想了解这里发生了什么。

node.js next.js electron node-modules
1个回答
0
投票

查看错误,它正在寻找

src\lib\addon.node
中的插件,根据您共享的树,这不是插件所在的位置。

Error: Cannot find module 'C:\Users\User\Documents\Projecten\NextJSIntro\my-first-nextjs-app\src\lib\addon.node'

依赖 CWD 来访问文件可能不是个好主意。相反,您可以从相对路径加载它,因此唯一重要的是它相对于加载它的代码的存储位置,而不是它的运行位置:

const addonPath = '../../../lib/addon.node';
© www.soinside.com 2019 - 2024. All rights reserved.