How to use canvas for node.js on heroku - node.js

The error I get is the following:
Error: Cannot find module 'canvas'
Require stack:
- /app/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
at Function.Module._load (internal/modules/cjs/loader.js:840:27)at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/app/index.js:6:16)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/index.js' ]
When I tried local it worked fine, I started having problems after I uploaded the code on Heroku. I've searched online but seems like all answers on google for this question are outdated. These are my current dependencies:
"dependencies": {
"canvas": "^2.6.1",
"discord.js": "^12.2.0",
"node": "^14.2.0"
}
And this is my code:
const Discord = require('discord.js');
const { prefix } = require ('./config.json');
const client = new Discord.Client();
const Canvas = require('canvas');
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
// Do something.
}
Anyone that could help with it?

First, with Heroku, you don't need to upload your node_modules on GitHub or something else. You can also try in the terminal :
npm start
Then :
npm install discord.js canvas node
If it isn't enough, check in your package.json file if there is :
"scripts": {
"start": "node index.js"
}

Related

Node script "Cannot find module"

I have the following file structure:
/avro.js
/node_modules/...
/package.json
/package-lock.json
I have run:
npm install javro yargs
In my avro.js I do:
#!/usr/bin/env node
const { javro, SchemaRegistryAvroFetcher } = require('javro');
const yargs = require("yargs");
const options = yargs
.scriptName("javro")
.positional('inpath', {
type: 'string',
describe: 'path to JSON Schema file to convert'
})
.positional('outpath', {
type: 'string',
describe: 'path to save converted Avro Schema'
})
.help()
.argv;
When I try to run the script I get:
./avro.js --help
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'javro'
Require stack:
- /Users/anentropic/Documents/Dev/Personal/myproj/avro.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Users/anentropic/Documents/Dev/Personal/myproj/avro.js:2:46)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/anentropic/Documents/Dev/Personal/myproj/avro.js' ]
}
Node.js isn't my first language, but I thought this should work?

Test a Node.js Application with Mocha including ES6-Modules in Dependencies

I want to test my simple Node.js (v14.15.4) application with Mocha (v8.2.1).
I also include Babel to compile the ES6 modules in my code.
In particular, I use "#babel/core" (v7.12.10), "babel/register" (v7.12.10) to use the compiler with mocha, and "#babel/preset-env" (v7.12.11) for ES6 module support.
Everything works fine, until I include an external dependency in my code. In my case, I want to use bpmn-js (v8.2.0) which itself is ready for the use with ES6 modules (https://bpmn.io/blog/posts/2018-migrating-to-es-modules.html).
My project structure is as follows:
- bpmn
- index.js
- node_modules
- ...
- babel.config.json
- index.js
- index.test.js
- package.json
The files are depicted below:
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --require #babel/register index.test.js"
},
"devDependencies": {
"mocha": "8.2.1",
"#babel/core": "7.12.10",
"#babel/register": "7.12.10",
"#babel/preset-env": "7.12.11"
},
"dependencies": {
"bpmn-js": "8.2.0"
},
"author": "",
"license": "ISC"
}
babel.config.json
{
"presets": ["#babel/preset-env"]
}
index.js
export {
default
} from './bpmn';
bpmn/index.js
//import { Viewer } from 'bpmn-js';
export default (str) => {
return str.toUpperCase();
};
index.test.js
import toUpperCase from './index';
const assert = require('assert');
describe('The module toUpperCase', () => {
it('should transform my test string', () => {
assert.strictEqual(toUpperCase('test'), 'TEST');
});
});
As stated, everything works fine when running npm run test.
However, if I include the comment in line 1 in the bpmn/index.js file,
the error below occurs. It seems that babel compiles my local project files, but ignores the external dependencies, i.e. bpmn-js in that case
\node_modules\bpmn-js\index.js:1
export {
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Module._compile (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (D:\Code\Neuer Ordner\bpmn\/index.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (D:\Code\Neuer Ordner\/index.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (D:\Code\Neuer Ordner\/index.test.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .js] (D:\Code\Neuer Ordner\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (D:\Code\Neuer Ordner\node_modules\mocha\lib\esm-utils.js:20:12)
at Object.exports.loadFilesAsync (D:\Code\Neuer Ordner\node_modules\mocha\lib\esm-utils.js:33:34)
at Mocha.loadFilesAsync (D:\Code\Neuer Ordner\node_modules\mocha\lib\mocha.js:431:19)
at singleRun (D:\Code\Neuer Ordner\node_modules\mocha\lib\cli\run-helpers.js:125:15)
at exports.runMocha (D:\Code\Neuer Ordner\node_modules\mocha\lib\cli\run-helpers.js:190:10)
at Object.exports.handler (D:\Code\Neuer Ordner\node_modules\mocha\lib\cli\run.js:362:11)
at D:\Code\Neuer Ordner\node_modules\yargs\lib\command.js:241:49
There are two problems in your approach. The first one, really fundamental, is that you are trying to import bpmn-js inside a Node.js process. This package can only be run in a browser as it depends on the DOM Document being available. You are never going to be able to do this. The other problem is, as you correctly figured out, that bpmn-js uses ES6 module syntax. By default, packages found in the node_modules directory are ignored by #babel/register that you require. In other words, they are not transpiled. You could configure #babel/register to make an exemption for bpmn-js and transpile it but your import would still fail with something like:
ReferenceError: document is not defined

Node.js Mocha Error: ReferenceError: test is not defined

I've got an error after running test scripts.
app.test.js
const { app } = require('../src/app');
describe('Basic functionality', () => {
test('App should receive a command to run', () => {
expect(app('1'))
.toEqual('Run command 1');
});
test('Should return instructions when no command is given', () => {
expect(app())
.toEqual('Usage: node index.js <COMMAND>');
});
test('Should throw an error when no such command exists', () => {
expect(() => {
app('100')
})
.toThrow('No such command exists');
});
});
package.json (I've installed mocha in my project. I've tried to install mocha globally, but the result was same)
"scripts": {
"test": "mocha __tests__/app.test.js"
},
"devDependencies": {
"mocha": "^8.0.1"
}
I've got an error after running
yarn run test
Below is the error code
ReferenceError: test is not defined
at Suite.describe (/test/__tests__/app.test.js:4:2)
at Object.create (/test/node_modules/mocha/lib/interfaces/common.js:143:19)
at context.describe.context.context (/test/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Object.<anonymous> (/test/__tests__/app.test.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.exports.requireOrImport (/test/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/test/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/test/node_modules/mocha/lib/mocha.js:421:19)
at singleRun (/test/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/test/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/test/node_modules/mocha/lib/cli/run.js:366:11)
at innerArgv.then.argv (/test/node_modules/yargs/lib/command.js:241:49)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Please teach me where I went wrong.
I solved it by specifying the route:
Like this:
npx mocha "src/testing-examples/index.test.js" --recursive --require #babel/register
I hope you find it useful :)

node.js module not found in subfolder

I am building a basic web scraper using node, express, and puppeteer, and when I try to run node index.js. I get this error
alexskreen#Alexs-MacBook-Air WOD-Scraper2 % node server/index.js
internal/modules/cjs/loader.js:960
throw err;
^
Error: Cannot find module './server/scrapers'
Require stack:
- /Users/alexskreen/Desktop/WOD-Scraper2/server/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
at Function.Module._load (internal/modules/cjs/loader.js:840:27)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js' ]
}
Before adding my require statements everything is working:
const express = require("express");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
const scrapers = require('./server/scrapers');
const db = require('./server/db');
If module is in your subfolder you should use,
const something = require("./subfolder/module");
If it is not a subfolder module then make sure it is installed in node_modules.

Error: Cannot find module 'camelcase'

I'm trying to run a gulp based application.
On Ubuntu, everything is working, but on Debian server not.
Ubuntu: npm: 5.3.0, nodejs: v8.2.1, bower#1.8.0, gulp-cli#1.4.0, npm#5.3.0
Debian: npm: 5.3.0, nodejs: v8.2.1, bower#1.8.0, gulp-cli#1.4.0, npm#5.3.0
Gulpfile:
'use strict';
const gulp = require('gulp');
const sass = require('gulp-sass');
gulp.task('scss2css', function () {
gulp.src(['./web/scss/style.scss'])
.pipe(sass({outputStyle: 'compressed'}))
.pipe(gulp.dest('./web/css/'));
});
gulp.task('scss2css:watch', function () {
gulp.watch('./scss/**/*.scss', ['scss2css']);
});
gulp.task('default', function () {
gulp.start('scss2css');
});
When I type "gulp", I receive message like below:
module.js:487
throw err;
^
Error: Cannot find module 'camelcase'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/gulp-cli/node_modules/yargs/lib/parser.js:3:17)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
I just removed nodejs package and directories:
/usr/lib/node_modules/
/usr/local/lib/node_modules/

Resources