Код: Выделить всё
└── my-electron-project/
├── lib/
│ └── addon/
│ ├── build/
│ │ └── Release/
│ │ └── addon.node
│ ├── addon.cc
│ ├── binding.gyp
│ ├── index.js
│ └── package.json
├── main/
│ ├── main.js
│ └── preload.js
├── src/
│ └── app/
│ ├── page.tsx
│ └── api/
│ └── hello/
│ └── route.ts
└── package.json
Я получаю сообщение об ошибке. Ошибка анализа модуля: неожиданный символ '`' (1:2)
для моего дополнения:
addon.cc
Код: Выделить всё
#include
#include
napi_value Addon(napi_env env, napi_callback_info info) {
napi_status status;
napi_value args[1];
size_t argc = 1;
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
if(status != napi_ok) {
return nullptr;
}
char buf[128];
size_t buf_size = 128;
size_t copied_size = 128;
status = napi_get_value_string_utf8(env, args[0], buf, 128 + 1, &copied_size);
if(status != napi_ok) {
return nullptr;
}
napi_value output;
status = napi_create_string_utf8(env, buf, copied_size, &output);
if(status != napi_ok) {
return nullptr;
}
return output;
}
#define DECLARE_NAPI_METHOD(name, func) {name, 0, func, 0, 0, 0, napi_default, 0}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("Addon", Addon);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init);
Код: Выделить всё
{
"targets": [
{
"target_name":"addon",
"sources": [
"addon.cc"
]
}
]
}
Код: Выделить всё
const addonNative = require('bindings')('Addon')
console.log(addonNative.Addon("test"))
Код: Выделить всё
npm configureКод: Выделить всё
nmp buildКод: Выделить всё
node index.jsКроме того, я запускаю:
Код: Выделить всё
node-gyp rebuild --target=20.12.2 --arch=x64 --dist-url=https://atom.io/download/atom-shellpage.tsx:
Код: Выделить всё
"use client";
import { useEffect, useState } from "react";
export default function Home() {
const [message, setMessage] = useState("default");
const getData = async () =>
fetch("/api/hello")
.then(function (response) {
return response.text();
})
.then(function (data_raw) {
const data = JSON.parse(data_raw);
setMessage(data.message);
});
useEffect(() => {
getData();
}, []);
return (
{message}
);
}
Код: Выделить всё
import { NextResponse } from "next/server";
const addon = require('/lib/addon/addon.node')
export async function GET() {
console.log(process.version)
try {
const result = addon.Addon("test");
return NextResponse.json({
message: result
});
} catch(error) {
return NextResponse.json({
message: error.message
});
}
}
Код: Выделить всё
npm run devКод: Выделить всё
electron-nextjs-project@0.1.0 dev
> concurrently -n "NEXT,ELECTRON" -c "yellow,blue" --kill-others "next dev" "electron ."
[ELECTRON]
[ELECTRON] [9564:0716/112012.339:ERROR:web_contents_preferences.cc(240)] preload script must have absolute path.
[NEXT] ▲ Next.js 14.2.4
[NEXT] - Local: http://localhost:3000
[NEXT]
[NEXT] ✓ Starting...
[NEXT] ✓ Ready in 17.4s
[NEXT] ○ Compiling / ...
[NEXT] ✓ Compiled / in 966ms (490 modules)
[NEXT] GET / 200 in 1166ms
[NEXT] ✓ Compiled in 151ms (239 modules)
[NEXT] ⨯ ./lib/addon/addon.node
[NEXT] Module parse failed: Unexpected character '�' (1:2)
[NEXT] 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
[NEXT] (Source code omitted for this binary file)
[NEXT]
[NEXT] Import trace for requested module:
[NEXT] ./lib/addon/addon.node
[NEXT] ./src/app/api/hello/route.ts
[NEXT] ⨯ ./lib/addon/addon.node
[NEXT] Module parse failed: Unexpected character '�' (1:2)
[NEXT] 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
[NEXT] (Source code omitted for this binary file)
[NEXT]
[NEXT] Import trace for requested module:
[NEXT] ./lib/addon/addon.node
[NEXT] ./src/app/api/hello/route.ts
[NEXT] ○ Compiling /_not-found ...
[NEXT] ⨯ ./lib/addon/addon.node
[NEXT] Module parse failed: Unexpected character '�' (1:2)
[NEXT] 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
[NEXT] (Source code omitted for this binary file)
[NEXT]
[NEXT] Import trace for requested module:
[NEXT] ./lib/addon/addon.node
[NEXT] ./src/app/api/hello/route.ts
[NEXT] ⨯ ./lib/addon/addon.node
[NEXT] Module parse failed: Unexpected character '�' (1:2)
[NEXT] 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
[NEXT] (Source code omitted for this binary file)
[NEXT]
[NEXT] Import trace for requested module:
[NEXT] ./lib/addon/addon.node
[NEXT] ./src/app/api/hello/route.ts
[NEXT] GET /api/hello 500 in 2434ms
[NEXT] GET /api/hello 500 in 21ms
[NEXT] GET / 500 in 9ms
Подробнее здесь: https://stackoverflow.com/questions/787 ... -character
Мобильная версия