Force node to require coffeescript by default - node.js

It's possible to use coffeescript files without compiling it on node, not directly of course but after importing "cofffee-script/register". I am wondering though if it's possible to feed coffeescript file to node directly, forcing it to import "coffee-script/register" by default? so I could do this from the command line
node debug my-app.coffee
Maybe somehow tweaking require.cache?

Related

NodeJS require doesn't work. Cannot import downloaded npm modules

I have a slight problem with a basic Node.JS method. When I'm trying to use "require()' method to import any downloaded (with 'npm install ..) module/library, I get a Visual Studio Code message that asks 'to convert 'require'(which is a Common JS module) into ES. If I do it, it transforms 'require()' into 'import..' though I want keep using 'require()'. Why is it so ? I read somewhere that Common JS is already included in the Node.JS environment though.
Then, when trying to compile my index.js file (with 'node index.js'), I obviously get an error about 'require' not recognized method.
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users...index.js from C:\Users...index.js not supported.
I tried then to install Webpack to fix this issue, but nothing change. I also kind of installed CommonJS (npm i common-js)..
Another common answer was to remove 'type':'module' from package.json file which normally should allow to use 'require', but I don't even have such a line in the file.
On top of that I've recently read that 'require' is not compatible with browser development tools (it's ok). But I'm trying to import a downloaded module/npm package in VSC terminal, not even in a browser.
As you understand I'm new to Node.JS, but I don't really get what's going on in this case...

Ignore filename extension for child_process fork using node-dev and Typescript

I have a project written in Typescript, and I'm using node-dev alongside ts-node in my local enviroment for development. I'm using child_process's fork method to instantiate a subprocess, like so:
fork(path.join(__dirname, './worker.ts'));
This works fine, and I can even set breakpoints in VS Code for the worker.
The problem is that when building (transpiling) my project, a MODULE_NOT_FOUND exception is thrown because worker.ts turned into worker.js. Initially, mi idea was to omit the file extension when forking (fork(path.join(__dirname, './worker'));), but if I do so, when running the project with node-dev, it throws a MODULE_NOT_FOUND because it can't resolve the file if the extension is not present.
Is there any workaround for this? Maybe an extra configuration option for node-dev?
I'm on Windows 10 using node v12.22.1
A simple solution I suggest would be to read in the current file type. The compiled files should end with ".js" while the source files end with ".ts". Using the path.extname methode in combination with __filename you can easily extract that file extension and simply concat it to your file name.
fork(path.join(__dirname, './worker' + path.extname(__filename)));

How to I run in nodejs a javascript file produced by haste?

If I have a file, hello.hs:
main = putStrLn "Hello, World!"
I can compile it to hello.js using haste command:
hastec hello.hs
How do I run the result hello.js file under nodejs?
By default, Haste's main is set to execute when the browser's onload event fires. This obviously makes no sense for Node, so you need to pass the --onexec flag to Haste when compiling your program:
$ hastec --onexec hello.hs
Haste uses Node to run its test suite in this way. Note, however, that with the exception of writing to standard output (like putStrLn), Haste does not map system operations (file IO, etc.) to Node equivalents. If you're writing an application that needs to interact with the OS, you're better off using vanilla GHC.
Update:
Thank you, and good answer. To recap, if you want to compile and run hello.hs under node, the two lines would be:
hastec --onexec hello.hs
node hello.js
haste doesn't know about node, so the .js files in produces doesn't export anything.
You need to somehow augment the .js file to export the functionality that you want, namely you need to export the hasteMain() function.
You may try the --with-js command line option to hastec
Or you may simply append the following line to the end of your hello.js file:
module.exports = hasteMain;
Once you do that, you can load hello.js as a module using require and run the code:
hasteMain = require('./hello.js');
hasteMain();
You may also want to look at ghcjs. The React team recently moved one of the modules from haste to ghcjs

Is it necessary to install `node-jsx` if we use reactify

In my Nodejs application, I use React. I browserify + reactify to transform .jsx files to js files.
I use this line of code in my entry js file :
require('node-jsx').install();
I noticed that node-jsx is deprecated. Do I still need to have this line if I already reactify my react files ?
No, it is not necessary to use either node-jsx or JSXTransformer when using reactify. Reactify does all the transforms needed.
Most likely the reason node-jsx is deprecated is because it is a very bad idea to compile at runtime, as the browser has to download the entire compiler and the compiler will take time to load. Always pre-compile JSX, babel, and coffeescript.

CoffeeScript source maps for nodeJS development

Now that CoffeeScript supports the new Source Map hotness, I was wondering if I can also use the source maps not just in my browser, but on the command line while developing my nodeJS apps.
I want the JS compiler to give me more useful error traces where the lines actually match with my coffeescript files instead of compiled JS files.
The source-map-support module does this, just install the module and put this at the top of your code:
require('source-map-support').install()
Now with CoffeeScript 1.6.2 it just "works" if you run your app with the coffee command :)
Until coffee-script gets some better support for require(), try this:
https://npmjs.org/package/coffee-script-mapped

Resources