I built an API that uses the canvas module on the server using Sveltekit.
Everything was working properly when I was writing and testing the code.
However, when I perform npm run build
, I get the following error message:
> [email protected] build
> vite build
vite v5.4.11 building SSR bundle for production...
✓ 194 modules transformed.
x Build failed in 4.78s
error during build:
[commonjs--resolver] ../../Common/node_modules/canvas/build/Release/canvas.node (1:2): Unexpected character '�' (Note that you need plugins to import files that are not JavaScript)
file: abc/Common/node_modules/canvas/lib/context2d.js:1:2
1: MZ�♥♦���@►☺♫▼�♫� �!�☺L�!This program cannot be run in DOS mode.
^
2: $�������������
3: �����
at getRollupError (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
at ParseError.initialise (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/node-entry.js:13286:28)
at convertNode (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/node-entry.js:14997:10)
at convertProgram (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/node-entry.js:14240:12)
at Module.setSource (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/node-entry.js:15983:24)
at async ModuleLoader.addModuleSource (file:///abc/Projects/nudity_detector/node_modules/rollup/dist/es/shared/node-entry.js:19879:13)
I added
build: {
rollupOptions: {
external: [
"canvas",
]
}
},
to the vite.config.ts file and tried to rebuild and now I’m getting this error:
> [email protected] build
> vite build
vite v5.4.11 building SSR bundle for production...
src/routes/api/nude/+server.ts (1:14): "RequestEvent" is not exported by "node_modules/@sveltejs/kit/src/exports/index.js", imported by "src/routes/api/nude/+server.ts".
✓ 463 modules transformed.
Generated an empty chunk: "entries/endpoints/api/nude/multipart/_server.ts".
node:internal/event_target:1100
process.nextTick(() => { throw err; });
^
ReferenceError [Error]: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'abcProjectsnudity_detectorpackage.json' contains "type": "module". To treat it as a CommonJS script, rename it to u
se the '.cjs' file extension.
at file:///abc/Projects/nudity_detector/.svelte-kit/output/server/entries/endpoints/api/nude/_server.ts.js:268:61
at file:///abc/Projects/nudity_detector/.svelte-kit/output/server/entries/endpoints/api/nude/_server.ts.js:425:3
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
at async analyse (file:///abc/Projects/nudity_detector/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:84:62)
at async MessagePort.<anonymous> (file:///abc/Projects/nudity_detector/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:326:10)
at [kOnMessage] (node:internal/worker:337:37)
at MessagePort.<anonymous> (node:internal/worker:232:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:826:20)
at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)
Node.js v20.12.2
I have not used __dirname
in the project.
How do I fix these issues?