-3
  1. Okay so I'm new to Javascript, please keep that in mind, and I'm working from a directory in my home computer, like, the web is not hosted in a server or anything, I've never done that

I've been trying to work with a Google Translate API (https://github.com/matheuss/google-translate-api)

I literally just copied the first example, and the console log on my web said something like

Uncaught ReferenceError: require is not defined file:///C:/Users/User/Desktop/web/script.js:11

For context, here's the line

const translate = require('google-translate-api');

Besides that whenever I try and install the api using the given Command on the Github page, I get some errors

npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\User\package.json'

also

$npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\User\package.json'
$npm WARN User No description
$npm WARN User No repository field. $npm WARN User No README data $npm WARN User No license field.

and

$found 6 vulnerabilities (2 high, 4 critical)

When dealing with it by using $npm audit all I get is

$npm ERR! code EAUDITNOPJSON

$npm ERR! audit No package.json found: Cannot audit a project without a package.json json $npm ERR! A complete log of this run can be found in:

$npm ERR! C:\Users\User\AppData\Roaming\npm-cache_logs\2020-07-09T16_40_06_962Z-debug.log

and the same goes for $npm audit fix

  1. I've tried to follow the guidelines I found on Github, even downloading the files directly to my project's folder (again, I don't really know if that would be useful at all), and nothing worked. I have GitHub on my PC, Git, nodejs, npmjs etc. and nothing works.

I've also used $npm init as sugested below (which created a package.json file) and then installed the API, but the console log part where it says

Uncaught ReferenceError: require is not defined file:///C:/Users/User/Desktop/web/script.js:11

still happens

I've read somewhere, however, that I should use something like browsify because the problem might be related to the fact that my web isn't hosted on a server (again, idk the technical words, but I'm trying to explain my situation as best as I can), but maybe I'm just making a really simple mistake.

  1. Here's some code that I copied directly from the API's Github page (textoEntrada is just some text I put inside the thing from an input text box)
    translate(textoEntrada, {to: 'en'})
    .then(res => {
    console.l`enter code here`og(res.text);
    console.log(res.from.language.iso);
    }).catch(err => {
    console.error(err);
    });```
  • 1
    [`require()` is not part of the standard JavaScript spec](https://stackoverflow.com/questions/9901082/what-is-this-javascript-require#:~:text=Now%20require()%20is%20a,the%20system%2Dwide%20search%20path.). You have to run your script with the Node.js (or similar) runtime (e.g., `node index.js`) - otherwise as you've alluded you'll need to run your package through `browserify` or a similar compiler. – esqew Jul 09 '20 at 18:17
  • 1
    In other words, Node modules are not guaranteed to be browser-compatible out of the box, and all major browsers do not support the `require` method. You can sometimes reference a Node module using a `script` tag, but there are some conventions the modules might employ that would prevent them from functioning as intended in the browser context. – esqew Jul 09 '20 at 18:25

1 Answers1

0

you firstly need to make sure that you initialized an npm package by execute this command on command line:

npm init -y 

then you will be able to install any packages.

also don't forget to get a walk on the npm docs, it definitely will help you later.

Fawzi
  • 71
  • 1
  • 5
  • I've tried that and all, but the console error still occurs – Francisco Javier Jul 09 '20 at 18:09
  • @FranciscoJavier Can you elaborate as to how this answered your question that you've marked it as accepted? It would be helpful for future visitors to this question. – esqew Jul 09 '20 at 18:17
  • if you please, double check that it's all in the same directory, the package json exists and node_modules folder exists, if it's the case, the error shouldn't occur. – Fawzi Jul 09 '20 at 18:17
  • and yes as @esqew said, it seems your question doesn't solved yet, it shouldn't be marked as accepted answer if it didn't solve your problem. – Fawzi Jul 09 '20 at 18:19
  • that's true, I'm not sure on marking it as accepted or not since technically I edited it to fit my new proble, I gave it an applause. @esqew in regards to my now problem, yes, all of the node_modules are still present but I'm now having a problem with npm (numbers 337, 1033 and 1322 according to npm audit) – Francisco Javier Jul 09 '20 at 18:36
  • ok, so the answer solved the main problem, the other problem can be posted on another question with more information, so we will be able to help, it's ambiguous now. – Fawzi Jul 09 '20 at 18:47