I've tried using exec with a bash file, I've tried using direct calls, I've tried digging into npm and npx docs but there seems to be no answer to this question: how can an npx call be triggered form node.js code?
An answer that triggers the same functionality as an npx call without actually being written as an npx call would also be acceptable.
I am not really sure where your problem is...
how you call npx scripts in general?
running shell commands from within node?
Running specifically npx commands? Is there an error you get?
The most widespread way to run shell commands would be child_process, and I don't know why you couldn't just put an npx command in there (I just tested it with npx --help and that worked):
const { exec } = require("child_process");
exec("echo Hello World", (err, stdout, stderr) => {
if (err) {
console.error();
console.error("Error:");
console.error(err);
console.error();
}
console.log(stdout);
console.error(stderr);
});
Now, this might be what you meant by "tried using exec", and you just get an error doing that. Of course, it would be helpful to have that error in that case, but maybe it is that npx isn't found.
Make sure you have npx installed then - maybe it's just npms virutal environment that can't access it, so you could use npm install npx to install it into there. If you are running it with npm, make sure it's installed globally: npm install -g npx.
If it is a different problem, please provide more information on what exactly you are lacking or potential errors you are getting.
Related
I am trying to run my node.js app in Git bash terminal. It worked before every time I type nodemon server.js. Recently, I upgrated my node.js and npm. If I try to run my app on Git Bash again, an error of
C:\Users\PC\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found
Shows up.
I tried uninstalling and installing nodemon with npm install -g nodemon, and reinstalled git, the error still persists. Although, nodemon works fine on windows cmd terminal.
It would be nice to make it work on Git Bash terminal.
it actually comes down to the place you installed the Nodemon tool.
most people will install it in their working directory but i found that this way is not a safe bet. you should install it in the root directory as it will not cause trouble once you try to use it in another project.
Also facing the same issue! But I found a way around using npx nodemon script.js
Although I found the problem. Git bash is trying to execute nodemon from this directory: C:\Users\username\AppData\Roaming\npm/node_modules/node/bin/node
But in this path nodemon doesn't exist.
The path should be C:\Users\username\AppData\Roaming\npm\node_modules\nodemon
I'm trying to check if a particular npm package is installed/available globally using Nodejs. I managed to list the global dependencies/packages through the npm command npm list -g --depth=0. So i tried out this piece of code.
const {exec} = require("child_process");
exec("npm list -g --depth=0",(err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
Then it throws an error like this
PS: i also tried to use npm list -g --depth=0 | grep nodemon but i cannot use it on command prompt. So that is wrong in the above code? how can check whether if package installed globally in any OS using nodejs?
The code in your npm.js file looks OK and should run successfully - that's assuming that:
when you run npm list -g --depth=0 directly in your CLI you do get the desired result.
The version of nodejs you're running does support ES6 features, such as Object Destructuring and Arrow functions.
The problem is the filenaming of the nodejs script - Don't name it npm.js.
You need to rename the file as something else, such as, e.g. get-global-pkgs.js.
Then cd to the directory where get-global-pkgs.js resides, and run either of the following commands:
node get-global-pkgs.js
or
node get-global-pkgs
i.e without the .js suffix
Note: Naming the file npm.js only seems to be an issue when the files content utilizes exec() and/or spawn() methods, and the given command results in a http(s) GET request.
I'm playing around with a test Laravel app and I'm trying to run "npm run watch" but it just throws errors. Even with "npm install", it does the same thing. I'm on a Mac by the way. I just can't get anything to work and I have no idea why. If I've missed some information that you need, please tell me. Thanks in advance!
npm run watch - log file
npm install - error from terminal (log file was like 10k lines)
You have to try to uninstall the node.js and again install the nodejs(8.00.4 LTS version. Link("https://nodejs.org/en/download/")). Then to checking the node.js running properly or not go to the terminal and type command 'node -v' it shows the node.js installed version. Then try to implement the npm commands.
I've got everything working now. I just needed to change ownership of the node_module files. <3
Let me preface by stating that this is my first post, and also I can’t retrace my steps to where the create-react-app command no longer works in my Terminal. Last week, when I was practicing how to work with ReactJS I was able to use the command create-react-app and had been able to initialize a React project. However, I’ve been fiddling plentifully with NodeJS and NPM stuff simultaneously, and now when I call the create react-app command this is the output:
bash: create-react-app: command not found
I’m pretty sure it has to do with my $PATH, however I’m not experienced with making edits through vim
when editing my .bash_profile. In my attempts to diagnose and fix the problem, here is what I appended to my $PATH through vim:
export PATH="/usr/local/bin:$PATH"
Nevertheless, I know the order of $PATH matters, but I’m naive as to whether I should prepend or append the $PATH above?
Here is what is output when I echo $PATH:
/usr/local/heroku/bin:/usr/local/bin:/usr/local/sbin:~/bin:/Users/bjornjohnramos/.rbenv/bin:/Users/bjornjohnramos/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/bjornjohnramos/.npm-packages/bin/express
-> /Users/bjornjohnramos/.npm-packages/lib/node_modules/express-generator/bin/express-cli.js:/Users/bjornjohnramos/.npm-packages/lib:export
PATH="/usr/local/bin:$PATH":export
PATH=$PATH:/Users/my_name/.npm-packages/bin/
my node and npm versions:
node v6.9.1
npm 4.2.0
Who knows what happened? But maybe we can just fix it by reinstalling...
npm uninstall create-react-app
npm uninstall create-react-app -g
npm install create-react-app -g
The first two commands will uninstall create-react-app locally and globally. (Not sure how you may have installed it. Harmless to run both.) The third installs globally, and should set up the path for you as needed.
use command npx create-react-app my-app rather than using npx create-react-app my-app that will resolves your bash: create-react-app: command not found err on windows.
Hope this is helpful
Happy coding...
So I can run a js file like
$ node file.js
with node.io inside the file doing things.
but if i go to the command line and try to type in a built in module like it shows at https://www.npmjs.org/package/node.io
$ node.io query "http://www.reddit.com/" a.title
I just get back -bash: node.io: command not found
This is probably something incredibly stupid, but I can't figure it out.
This just means that node.io is not installed or if installed is not available in $PATH.
You can install it using the following command.
$ npm install -g node.io