In this paper,

Like + attention + favorites = learned


Developers with front-end development experience or experience with Node should know that if you need to run js files locally, you need to run them through Node xxx.js.

When executing vue create project-name to create a vue project, why doesn’t the command start with node?

This time I’ll take a look at why this command “doesn’t need” to be executed using Node.

The runtime environment for this article

  • Operating system: Win11
  • Node version: 17.6
  • NVM version: 1.1.9

Due to my daily work, I need to switch node versions using NVM.




Install the vue – cli

By default, you’ve already installed Node on your computer. If you haven’t, please go to the Node website and install it yourself.


Before running the vue create command, ensure that vuE-CLI is installed on the computer.

Vue-cli is a scaffolding tool for creating vUE projects. Vue-cli provides commands such as vue create.


Use the following command to install vue-CLI:

npm install -g @vue/cli
# or
yarn global add @vue/cli
Copy the code

After installing vuE-CLI, run vue –version to view the version of vue-CLI currently installed.




Where exactly is vuE-CLI installed?

Whether you install VUe-CLI using NPM or YARN, you need Node.

So I went to the Node installation directory and found Vue.


If you don’t know where Node is installed, after installing vue-cli, you can use the following command to check the location of vue-cli

# Windows users
where vue

# MAC users
which vue
Copy the code

Open the vue file and have a look. The contents are as follows

#! /bin/sh basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") case `uname` in *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;; esac if [ -x "$basedir/node" ]; then exec "$basedir/node" "$basedir/node_modules/@vue/cli/bin/vue.js" "$@" else exec node "$basedir/node_modules/@vue/cli/bin/vue.js" "$@" fiCopy the code

I found a path that was a little more glaring

$basedir/node_modules/@vue/cli/bin/vue.js
Copy the code

$basedir indicates the directory where the file resides.


You can see that the vue file under \nodejs is just a soft link pointing to /node_modules/@vue/cli/bin/vue.js.


I went to /node_modules/@vue/cli/bin/ and found the vue.js file.

This file has more than 200 lines of code and contains various instructions and configuration items detailing the configuration of VUE-CLI. But the purpose of this article is to unravel why you can execute JS files “without” using Node. Therefore, this article will not go into the details of the configuration.


We only need to look at the first line of the file.

#! /usr/bin/env node
Copy the code

#! /usr/bin/env means to find the global environment of the current system, and then add node. Find node in the environment variable globally, and execute the file through Node.




Hello World

With that in mind, should we be able to create a file that can be executed “without” using Node?

  1. Soft link, in the global environment to add an executable JS file.
  2. use#! /usr/bin/env nodeCall the environment variablenodeTo perform itself.


The second condition is simply to add a sentence to the header of the JS file. Note that it must be the first line of the file!


So how do you add a soft link to a file?

You can run the ln -s command (Git Bash is recommended for Windows users).

Ln -s File path command nameCopy the code


I created test.js on drive D with the following contents:

#! /usr/bin/env node

console.log('Hello World')
Copy the code


Then go to the node root directory and create the link using the sayhello command (I’m using sayhello here, you can customize it)

ln -s D:/test.js sayhello
Copy the code


The sayHello file (without the suffix) will then appear in the node root directory.


At this point, you can use the sayHello command anywhere using Git Bash.




Recommended reading

  • 👍 “Front-end Essential” Local Data Interface
  • 👍 18 Websites to Make your Background look cool