Node not requiring *.coffee files

2019-09-01 12:21发布

According, to https://stackoverflow.com/a/4769079/347915, I should be able to require a .coffee file from a .js file:

$ echo 'console.log "works"' > module.coffee

$ echo '
> require("coffee-script")
> require("./module")
> ' > test.js

$ node test.js
works

However, when I do that, I get this message when running test.js:

module.js:340
    throw err;
      ^
Error: Cannot find module './module'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (/Users/jose/Desktop/pedo/test.js:2:1)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  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:902:3

I'm using Node 0.10.25 installed using NVM. Also using CoffeeScript 1.7.1 installed using NPM 1.3.24.

1条回答
叼着烟拽天下
2楼-- · 2019-09-01 12:32

Since Coffeescript 1.7.0, the required library changed. Now you have to use:

require('coffee-script/register');

See the changelog: http://coffeescript.org/

查看更多
登录 后发表回答