以不同的文件扩展名运行node.js

3

是否可以使用不同于.js的扩展名来运行node.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内部要求这些文件。

1个回答

3

运行良好:

$ 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接