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
Related
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...
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)));
Im new to typescript. Im using a serverless solution from google (google cloud functions) that in the background is running Node.js with typescript. When I get an "runtime error" in the logs I see an error in a .js file, this makes sense since .ts code is compiled to .js but it makes debugging a lot harder as I write typescript code and not javascript code. In general I would like to see the line that produced the error in .ts and not in .js. Is this possible?
You will need a "source map" to map the JavaScript line numbers to TypeScript line numbers.
You can do this with the source-map-support node module. First install it in your project:
npm install source-map-support
Then add this to your TypeScript file (index.ts):
require('source-map-support').install()
Then, if you function crashes, the line numbers should show TypeScript source lines.
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.
I'm trying to figure out how to use external javascript libraries in the Atom editor. I used npm to install the momentjs library, since Atom uses node. However, I'm puzzled as to what to do now? I can't find a way to use the library in Atom.
I thought that I could go to the Atom init.coffee file and do a require "moment" (also tried require "momentjs") but nothing seems to work.
The whole reason behind this is so I can use some javascript libraries for formatting dates in a snippet (I have another SO question about that which I'll close if this solves it).
But this question is a general question about installing and running javascript libraries in Atom. I've looked through the Atom docs and Googled, but I can't find a good answer. I figured something like this would be pretty easy?
As Atom bundle its own node version (and thus is not using your global version(s)) it won't load globally installed modules through require.
However, the require method supporting absolute paths, you can still load any module if you know it's absolute path, which shouldn't be a problem in your specific case.
In your init script you can write:
momentjs = require('/path/to/momentjs')
But beware of modules that ships with binaries. Atom is using node 0.11.13 so if the module you're trying to require have been installed for a different version you'll get a Error: Module did not self-register.. In that case I'm afraid the only solution would be to install the module as a dependency of an Atom package (as suggested by #nwinkler).
You should be able to do the following when developing your own package:
Install moment using npm install --save moment - this will install the moment.js library as a dependency and register it in the package.json file
In your library, import it in your lib file:
moment = require 'moment';
myDate = moment().format();
Then you can use the moment object to format your timestamps.
All of this will only work if you're doing your own package, of course. Not sure if this will work with simple snippets as well.