使用不同的文件扩展名运行node.js
是可以运行node.js与扩展名不同.js
像: node server.type
而不是node server.js
?
我曾尝试require.extensions
require.extensions['.type'] = require.extensions['.js'];
但我得到这个错误:
Error: /root/project/server.type: invalid ELF header at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3
不过,我认为这与require
没有任何关系,除非我想在node.js中要求这些文件。
工作得很好:
$ echo 'console.log("hello world")' > server.type $ node server.type hello world
但我猜你想把它作为一个可执行文件运行。 在这种情况下,请使用以下命令启动脚本
#!/usr/bin/env node # followed by your actual script: console.log("hello world");
并使您的脚本可执行:
chmod 755 server.type
之后:
$ /root/project/server.type hello world