Axios doesn't load

I have the following line of code in my node program that doesn’t work:

const axios = require(‘axios’);

I get the error:

require.js:168 Uncaught Error: Module name “axios” has not been loaded yet for context: _. Use require()

I ran the commands:

yarn add axios
npm install axios --save -g

but it still doesn’t find the module.

And when I try to get the version number with >axios -v, I get the error:
‘axios’ is not recognized as an internal or external command,
operable program or batch file.

It thinks you’re using a library called RequireJS, which allows modules to be loaded in browsers.

Also this:

yarn add axios
npm install axios --save -g

First one installs and saves it locally to your project using the Yarn package manager, second one installs it globally (there should never be a reason to do this) using the NPM package manager. Pick one package manager, and dependencies should be local. Globals are generally command line scripting tools, and for that you should just use NPMs npx command (* regardless of if you use yarn or npm atm – yarn 2 gets the command and removes the ability to install globally, but it’s beta software ATM)

I don’t know that yarn add actually installs anything to node_modules – you might need to run yarn (without args) to make that happen. Been a while since I used yarn, so I don’t remember.

You don’t need to install axios globally. In fact, never install libraries globally, only commands – and not even those if you’re just using them as part of your project build.

Aside, it’s never a good idea to mix npm and yarn, though in this case npm wasn’t operating on your project, so you weren’t really mixing them.

Yeah yarn add installs as a dependency to node_modules (or whatever exists instead of node_modules in yarn 2) and adds the entry to package.json

ThanX for your input

axios is a library, it’s not an executable program. The reason you get an error with this is that you’re trying to invoke it like a program.

You need to explain what your setup is and what you’re trying to do. As I say, the other error is created by a library called RequireJS. This was a relatively commonly used library about 5-6 years ago. Lots of things still use it in some way. So it may well be that something you are using depends on it under the hood. But can’t tell from what you’ve posted what the issue is tbh, need more info

Node has its own resolver for requires, so the only reason you’d be seeing a requireJS error is if you’re running in a browser. If you’re writing browser code, ditch RequireJS and use a proper module bundler like Rollup or Webpack, which processes requires statically. Plus you can write actually modern JS that way without worrying (much) about browser support.

I was using it for a Web scraper but I found a better code source at https://www.freecodecamp.org/news/the-ultimate-guide-to-web-scraping-with-node-js-daa2027dcd3/ so I don’t have that problem any more.