I wanted to use mpv option for my Node project, but it seems the option is reserved already.
$ npm --mpv
output : 6.5.0
what's the mpv option pointing?
and is there any way I can use the mpv option for my Node project?
TLDR
You can use --mpv as an argument to your node.js project. There is nothing that would make it not work.
Example:
// example.js
if (process.argv[2] == '--mpv') {
console.log('hooray');
}
The script above would print hooray if you run it with --mpv as argument:
$ node example.js --mpv
hooray
Details:
The npm command treats -- options the same as - options for some reason. Thus the command:
npm --mpv
is somehow interpreted as
npm -m -p -v
Neither m nor p does anything and are ignored instead of throwing errors. But the v option prints out the version number. Thus the command is the same as
npm -v
Note that npm is not node.js. Node does not behave the same and would throw an error:
$ node --mpv
node: bad option: --mpv
Indeed, the npm project explicitly declares themselves to not be a node package manager even though npm is now managed by the node.js project directly. Npm can deploy packages written in other languages such as Ruby (such as the original Sass project, now re-written in js), Python or even C and assembly.
--mpv is not a reserved option -- nor is any other sequence. node itself takes many options, but anything on the command line after your script name is passed directly to process.argv.
In other words, the following have two different behaviors:
$ node --inspect script.js # enables node's built-in debugger; process.argv = ['node', 'script.js']
$ node script.js --inspect # process.argv = ['node', 'script.js', '--inspect']
Also, --mpv does not appear anywhere in the npm documentation. I suspect that the npm CLI is misinterperting --mpv as -v and printing its version number.
Related
I am trying to use Makefile to build Node projects. As a first step, I want to set node version using NVM. I tried the following code for that:
.PHONY: viz-ui
viz-ui:
. ${HOME}/.nvm/nvm.sh && nvm use 14.17.0
node -v
I set nvm use 16 before running make to test the output. After running make viz-ui, I get the following output:
. /home/vijayth2/.nvm/nvm.sh && nvm use 14.17.0
Now using node v14.17.0 (npm v6.14.13)
node -v
v16.14.2
In short, the node switch to 14.17.0 is not reflecting in the makefile, how can i fix it? Or what is the gap in my understanding of this?
Every line of a recipe is run in a brand-new shell. Therefore, when you reach node -v line, it is executed in a new shell that knows nothing about previous one that has set up different node version (this was executed in a separate shell and long gone).
You have basically two options:
Rewrite the recipe so that it all runs in a single shell invocation, e.g.:
.PHONY: viz-ui
viz-ui:
. ${HOME}/.nvm/nvm.sh && nvm use 14.17.0; \
node -v
or
Make use of .ONESHELL directive, so that all the recipe is executed in a single shell.
I know it is possible to switch between different versions of Node using NVM, n, or similar.
Is there a convenient way for the right version of Node to automatically be used when running commands within a given package? ("Right version" being determined by the engine tag or similar).
For instance, I would like to be able to do this:
cd mypackage-that-needs-node10
npm run serve
# ... node 10 is used
cd ..
cd mypackage-that-needs-node14
npm run serve
# ... node 14 is used
n supports an engine label to find Node.js version from package.json, and auto which includes more sources such as a .node-version file.
https://github.com/tj/n#specifying-nodejs-versions
For example:
$ n install engine
# or run the target version of node
$ n run auto index.js
# or execute a command with that Node.js in path
$ n exec auto npm run serve
A possible approach is to install node itself into your package, and have npm run scripts use it in preference to the system version of node. See node
I tried to run this command npx create-react-app my-app and got this error
error #typescript-eslint/eslint-plugin#4.5.0: The engine "node" is incompatible with this module. Expected version "^10.12.0 || >=12.0.0". Got "11.13.0"
error Found incompatible module.
The weird thing is I used node version 15.0.1 (latest) and yarn version 1.22.10 (latest) and the version that npx used Got "11.13.0" which does not exist on my machine.
Anybody face this problem ? Please help, many thanks in advance.
The situation may be different on Windows, but on UNIX-like operating systems (including macOS), you can find the path to the node being executed by npx with:
/usr/bin/env node -p process.execPath
This is because the npx file starts with #!/usr/bin/env node. So you can use /usr/bin/env node to execute the same node that npx will.
-p means "print the value" and process.execPath is the path to the executable.
Why do you want to do the above and not just use which node?
which will report aliases, which means that it will report the node executable you will see in your shell. That's not what npx will use, and that seems a likely possibility to explain why npx might use a different Node.js version than you expect.
$ alias node=echo
$ node foo
foo
$ which node
node: aliased to echo
$ /usr/bin/env node -p process.execPath
/Users/trott/.nvm/versions/node/v15.0.1/bin/node
$
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.
Whenever I run npm install I see that npm has been passed certain flags like NVM_CD_FLAGS how does nvm do this? does it actually have some other script in place of npm?
Looks like npm is not getting replaced
> which npm
/Users/welldan97/.nvm/versions/node/v8.5.0/bin/npm
> cat $(which npm)
#!/usr/bin/env node
;(function () { // wrapper in case we're in module_context mode
// windows: running "npm blah" in this folder will invoke WSH, not node.
/*global WScript*/
...
and the contents is the same as on npm github repo
looks like it sets it as environment variable
https://github.com/creationix/nvm/blob/master/nvm.sh#L226
> env | grep NVM
NVM_DIR=/Users/welldan97/.nvm
NVM_CD_FLAGS=-q
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NVM_IOJS_ORG_MIRROR=https://iojs.org/dist
NVM_BIN=/Users/welldan97/.nvm/versions/node/v8.5.0/bin
Then again, not sure where these flags go to, and if they affect npm