I'm trying to make a simple api with js. But i keep getting this stupid error. NOTE: This is my first time programming in js.
Code:
const express = require('express')
const app = express()
const { Canvas } = require('canvas-constructor')
const canvas = require('canvas')
app.get('/:feed', async (req, res) => {
const img = await canvas.loadImage('https://i.pinimg.com/originals/30/82/b0/3082b0354572c4d37af6994b4e8baa43.png')
let image = new Canvas(670, 435)
.printImage(img, 0, 0, 670, 435)
.setTextFont('28px Impact')
.printText(req.params.feed, 40, 150)
.toBuffer();
res.set({'Content-Type': 'image/png'})
res.send(image)//sending the image!
})
app.listen(8080)
Error:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/runner/ScarceExcellentVariables/node_modules/canvas-constructor/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)
at packageExportsResolve (node:internal/modules/esm/resolve:692:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
If anyone could help me i'd appreciate it
CodePudding user response:
The package you refer to requires you to pick what it should use to render graphics. You can import:
canvas-constructor/napi-rs
orcanvas-constructor/skia-canvas
orcanvas-constructor/canvas
But you can't import canvas-constructor
by itself. If you pick napi-rs
you would first need to install that package. A link to these three options are in the README file.
There are two types of packages:
- ES6 Modules (also used in browsers,
import x from "./x.js"
) - CommonJS (older but still supported,
x = require("./x.js")
.
The authors of canvas-constructor
made sure it works with both.
However, if you use require()
you may be missing a dependency named node:util
. In older Node versions it's only available when using import
.
Because of this, even though the package is listed as compatible with Node 14 and up, make sure you are using a recent version.
CodePudding user response:
https://www.npmjs.com/package/canvas-constructor
is an ES6 package, while you are using common js (nodejs default) you can either use a different package or modify your nodejs project to use ES6 standards.
To modify your project settings you may wanna follow this answer.