Closed thatfreakcoder closed 2 years ago
Hi, thanks for flagging. I've tried to reproduce this locally with express, but I'm getting the expected results. I've shared some example code below.
The line that's flagged in your error messages is using the Date()
function, which is native to JS, so I'm not sure why this would fail. Could you share some more info about your setup?
package.json
{
"name": "gns-sandbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.2",
"google-news-scraper": "^1.0.8"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
index.js
const gns = require('google-news-scraper');
const express = require('express')
const app = express()
app.get('/', async (req, res) => {
try {
const articles = await gns({
searchTerm: "dogecoin",
queryVars: {
hl:"en-US",
gl:"US",
ceid:"US:en"
},
prettyURLs: false,
timeframe: "1h",
puppeteerArgs: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
})
res.status(300).json(articles)
} catch (err) {
res.status(400).json(err)
}
})
app.listen(3000)
Command line:
nodemon index.js
Hi,
I am getting similar error.
I am running this as a Firebase Cloud Function (Typescript)
MY CLOUD FUNCTION CODE
exports.loadGoogleNewsScraper = functions.runWith({ memory: '1GB', timeoutSeconds: 300, }).pubsub.schedule('every 5 minutes').onRun(async (context) => {
console.log('loadGoogleNewsScraper runs every 5 minutes!');
const googleNewsScraper = require('google-news-scraper');
let articles = await googleNewsScraper({ searchTerm: "kaizer chiefs", prettyURLs: false, timeframe: "5d", puppeteerArgs: [ '--no-sandbox', '--disable-setuid-sandbox' ] }) console.log(articles)
return null; });
ERROR FROM FIREBASE CLOUD FUNCTION LOGS
TypeError: (intermediate value).toISOString(...).split(...)[0].replaceAll is not a function at module.exports (/workspace/node_modules/google-news-scraper/index.js:36:61) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async /workspace/lib/index.js:521:20
I am getting this hint from VS Code
"Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later."
I suppose a lot of people might experience the same issue. This is a problem for Typescript users
I could change my config to "ES2020", but then this breaks my entire project... I am currently on es2017
Can we perhaps change the line below to use anything else either than replaceAll
value: YES+cb.${new Date().toISOString().split('T')[0].replaceAll('-','')}-04-p0.en-GB+FX+667
,
I noticed that some compilers use Node 14 out of the box. Node 14 does NOT have support for the replaceAll function, it got supported after the Node 15 runtime.
The solution that worked for me is two of them.
Either change the replaceAll()
function with replace()
function (for Node 14 and below) and the regex expression will then be on line 36
value: `YES+cb.${new Date().toISOString().split('T')[0].replace(/-/g,'')}-04-p0.en-GB+FX+667`
Or Specify the Node Runtime in package.json
file under the "engines"
key like below:
"engines": {
"node": ">=15.0.0"
},
and add a .npmrc
file at the root folder where you have the package.json
file and write
engine-strict=true
Hope that helps @mashegoindustries
thatfreakcoder
Thank you. The first option you suggest looks like the option I will go for.
Don't you want to create a pull request. For this. Hopefully the author of the library will accept it. Like you "Some compilers use Node 14 out of the box"
@mashegoindustries I will submit a pull request for this, I opened this issue for the same :)
Merged in d711fb42a1a840308d12413b11ac713dff1124d4 thanks for contributing!
When running your code example. i am receiving this error message in the console:
I can work on a solution for it an can submit a PR if you want