The last article introduced you to the process of developing the NPM package. This article will share some of the problems I encountered in developing the NPM package. The first question is #! What exactly is /usr/bin/env node? (If THERE is any mistake in my understanding, welcome to give advice.)
When developing the NPM package, link the package to your project, and run the package to find an error.
#! /usr/bin/env node
Shebang
Shebang will be familiar to any front-end developer who has worked with Linux or Unix. Shebang is the name of a symbol, #! . This symbol is usually found at the beginning of the first line in Unix systems and is used to indicate the interpreter for the script file. Now that you know Shebang, you can understand that this line was added to specify that the script file should be executed using Node.
When you type a command, how does NPM recognize and execute the corresponding file? The specific principle has been described in NPM Scripts usage guide by Ruan Yifeng. When executing the script, we need to specify that the interpreter of the script is Node.
In some cases, even if you add this line, you may still encounter an error. Why?
No such file or directory
Copy the code
To solve this problem, you first need to look at /usr/bin/env. We already know that Shebang is used to specify the interpreter for your script, but different users or script interpreters may be installed in different directories, so how does the system know where to find your interpreter? /usr/bin/env tells the system to look it up in the PATH directory. So configure #! /usr/bin/env node, which allows the system to dynamically search for nodes to execute your script files.
Error: No such file or directory? Because your Node installation PATH is not added to the system PATH. So go ahead and configure the Node environment variable.
If you just want to test this, you can use the which node command to find your local node installation path and change /usr/bin/env to the node path you found.
One final point to note is that Windows does not support Shebang, which determines which interpreter to use to execute the script by the extension of the file.