I’ve been creating web scraping slack bot.
When I installed casperjs on yarn, then I ran the program below, I got the error below, that can’t find package “casperjs” imported from calculate.js. How can I fix it?
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '/bot-study/node_modules/casperjs/' imported from /bot-study/calculate.js
at new NodeError (node:internal/errors:399:5)
at legacyMainResolve (node:internal/modules/esm/resolve:235:9)
at packageResolve (node:internal/modules/esm/resolve:876:14)
at moduleResolve (node:internal/modules/esm/resolve:938:20)
at defaultResolve (node:internal/modules/esm/resolve:1153:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
calculate.js:
"use strict";
import bolt from "@slack/bolt";
import dotenv from "dotenv";
import chalk from "chalk";
import casperjs from "casperjs";
const casper = casperjs.create();
dotenv.config();
console.log(`SLACK_APP_TOKEN: ${process.env.SLACK_APP_TOKEN}`);
console.log(`SLACK_BOT_TOKEN: ${process.env.SLACK_BOT_TOKEN}`);
const app = new bolt.App({
token: process.env.SLACK_BOT_TOKEN,
appToken: process.env.SLACK_APP_TOKEN,
socketMode: true,
logLevel: "debug"
});
casper.start();
const urls = {
"MathA": [
"https://www.nnn.ed.nico/courses/1919/chapters/26869",
"https://www.nnn.ed.nico/courses/1919/chapters/26870",
"https://www.nnn.ed.nico/courses/1919/chapters/26871",
"https://www.nnn.ed.nico/courses/1919/chapters/26872",
"https://www.nnn.ed.nico/courses/1919/chapters/26873",
"https://www.nnn.ed.nico/courses/1919/chapters/26874",
"https://www.nnn.ed.nico/courses/1919/chapters/26875",
"https://www.nnn.ed.nico/courses/1919/chapters/26876"
],
"History": [
"https://www.nnn.ed.nico/courses/1859/chapters/24327",
"https://www.nnn.ed.nico/courses/1859/chapters/24328",
"https://www.nnn.ed.nico/courses/1859/chapters/24329",
"https://www.nnn.ed.nico/courses/1859/chapters/24330",
"https://www.nnn.ed.nico/courses/1859/chapters/24331",
"https://www.nnn.ed.nico/courses/1859/chapters/24332",
"https://www.nnn.ed.nico/courses/1859/chapters/24333",
"https://www.nnn.ed.nico/courses/1859/chapters/24334",
"https://www.nnn.ed.nico/courses/1859/chapters/24335"
],
"Home Economics": [
"https://www.nnn.ed.nico/courses/1919/chapters/26869",
"https://www.nnn.ed.nico/courses/1919/chapters/26870",
"https://www.nnn.ed.nico/courses/1919/chapters/26871",
"https://www.nnn.ed.nico/courses/1919/chapters/26872",
"https://www.nnn.ed.nico/courses/1919/chapters/26873",
"https://www.nnn.ed.nico/courses/1919/chapters/26874",
"https://www.nnn.ed.nico/courses/1919/chapters/26875",
"https://www.nnn.ed.nico/courses/1919/chapters/26876",
],
"Japanese": [
"https://www.nnn.ed.nico/courses/1847/chapters/24267",
"https://www.nnn.ed.nico/courses/1847/chapters/24268",
"https://www.nnn.ed.nico/courses/1847/chapters/24269",
"https://www.nnn.ed.nico/courses/1847/chapters/24270",
"https://www.nnn.ed.nico/courses/1847/chapters/24271",
"https://www.nnn.ed.nico/courses/1847/chapters/24272",
"https://www.nnn.ed.nico/courses/1847/chapters/24273",
"https://www.nnn.ed.nico/courses/1847/chapters/24274",
"https://www.nnn.ed.nico/courses/1847/chapters/24275",
"https://www.nnn.ed.nico/courses/1847/chapters/24276",
"https://www.nnn.ed.nico/courses/1847/chapters/24277",
"https://www.nnn.ed.nico/courses/1847/chapters/24278"
],
"art": [
"https://www.nnn.ed.nico/courses/1903/chapters/24092",
"https://www.nnn.ed.nico/courses/1903/chapters/24093",
"https://www.nnn.ed.nico/courses/1903/chapters/24094",
"https://www.nnn.ed.nico/courses/1903/chapters/24095",
"https://www.nnn.ed.nico/courses/1903/chapters/24096",
"https://www.nnn.ed.nico/courses/1903/chapters/24097",
],
"health": [
"https://www.nnn.ed.nico/courses/1901/chapters/24080",
"https://www.nnn.ed.nico/courses/1901/chapters/24081",
"https://www.nnn.ed.nico/courses/1901/chapters/24082",
"https://www.nnn.ed.nico/courses/1901/chapters/24083",
"https://www.nnn.ed.nico/courses/1901/chapters/24084",
"https://www.nnn.ed.nico/courses/1901/chapters/24085",
],
"biology": [
"https://www.nnn.ed.nico/courses/1889/chapters/24357",
"https://www.nnn.ed.nico/courses/1889/chapters/24358",
"https://www.nnn.ed.nico/courses/1889/chapters/24359",
"https://www.nnn.ed.nico/courses/1889/chapters/24360",
"https://www.nnn.ed.nico/courses/1889/chapters/24361",
"https://www.nnn.ed.nico/courses/1889/chapters/24362",
],
"English": [
"https://www.nnn.ed.nico/courses/1911/chapters/24146",
"https://www.nnn.ed.nico/courses/1911/chapters/24147",
"https://www.nnn.ed.nico/courses/1911/chapters/24148",
"https://www.nnn.ed.nico/courses/1911/chapters/24149",
"https://www.nnn.ed.nico/courses/1911/chapters/24150",
"https://www.nnn.ed.nico/courses/1911/chapters/24151",
],
"Physical Education": [
"https://www.nnn.ed.nico/courses/1897/chapters/24170",
"https://www.nnn.ed.nico/courses/1897/chapters/24171",
"https://www.nnn.ed.nico/courses/1897/chapters/24172",
],
"Spacial Activity": [
"https://www.nnn.ed.nico/courses/2025/chapters/25318",
"https://www.nnn.ed.nico/courses/2024/chapters/25317"
]
}
const percentages = [];
const login_id = process.env.LOGIN_ID;
const password = process.env.PASSWORD;
app.message(/pro-rate (.+)/i, async({message, say}) => {
const argument = message.text.match(/pro-rate (.+)/i)[0];
console.log(chalk.green(argument));
console.log(chalk.blue("First value of the retrieved category:", urls.argument[argument][0]));
casper.start("https://www.nnn.ed.nico/oauth_login?next_url=https://www.nnn.ed.nico/home&target_type=n_high_school_mypage", function(){
this.fillSelectors("form#oauth_identifier", {
'input[name="loginId"]': login_id,
'input[name="password"]': password
}, true);
});
casper.waitForUrl("https://www.nnn.ed.nico/home", function(){
for(let i = 0; i <= urls[argument].length; i++){
this.thenOpen(urls[argument][i], function() {
const progressRate = document.querySelector("li.sc-1yme7mc-3.eZrrkf").slice(3, 6).value;
console.log(chalk.blue("Progress Rate:", progressRate));
if(progressRate === "100") {
percentages.push(Number(progressRate));
}
percentages.push(Number(progressRate.slice(2, 3)));
});
}
});
casper.run();
let sum = 0;
percentages.forEach((value) => { sum += value });
say(`Current progress rate: ${sum / urls[argument].length}%`);
});
app.start();
“dependencies” part in package.json:
"dependencies": {
"@slack/bolt": "^3.17.1",
"@types/phantomjs": "^1.9.35",
"casperjs": "^1.1.4",
"chalk": "^5.3.0",
"dotenv": "^16.4.5"
},
I already tried:
- Install casperjs with Typescript type difinition like “@types/casperjs”
- import the casperjs with the code “import casperjs from “@types/casperjs”;”