Using an external node-installed JS library in Atom editor - node.js

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.

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...

NodeJS-MSMQ: SyntaxError: Cannot use import statement outside a module

I'm pretty new with NodeJS and I'm trying to use a module called "updated-node-msmq" (from this repository) in my project but getting an error.
What I've done:
I opened a new folder for the project.
I used the command "$ npm install --save updated-node-msmq" and got some warnings (Screenshot attached).
When I try to use the module according to the README, and run my "test.js" file I got the following error here in the screenshot).
I am using version 14.16.0 of NodeJS and after trying to fix this myself I realized that probably the problem is that the module was built for older versions of NodeJS.
I understand that low version is not a recommended solution, but I have no clue how to update a module.
Help / guidance in solving the problem?
Thanks in advance everyone!
Why are you using updated-node-msmq? That package seems like a mess, and the errors you are getting is because the whole module is written with ES2015 but doesn't seem to have been transpiled to node.js compatible code before published to NPM.
I'd advise you to use a tried and tested module for MSMQ first and foremost.
EDIT: Seems like the author fucked up in the 0.2.0 version. You could use the 0.1.9 (by instead using npm install updated-node-msmq#0.1.9 but that one is 3 years old.

Using moment.js (or moment-timezone.js) in TypeScript project

This is what I've done so far :
Install NodeJs and play with it : the node command is available
Install TypeScript using npm : npm install typescript -g
Now I am able to create .ts files, use the TypeScript syntax and compile the files using tsc command
Okay, from now on everything works fine. What I am doing is translating a Java library to TypeScript. But I am facing an issue : The Java library is using the Calendar object and I need the same kind of object in TypeScript.
I've searched a bit and found moment.js. I wanted to import that library into my TypeScript files.
Question : How do I do that ? I've looked around on StackOverflow but in every post I found there was something that made me think this would not be my solution, like :
Install typings / tsd : I've read that now moment.js has now a definition file (? sorry, maybe it is not the good term, but it has a moment.d.ts file)
Check in random.config.json file : I do not have such a configuration file (not in NodeJs in my guess, and in TypeScript I have the tsconfig.json file, and tried to include the moment.d.ts file but I wasn't able to import it in TypeScript > "cannot find module moment")
Install moment from npm : I've done it, but I didn't find out how to include the moment.d.ts file in my project.
and so on ...
I would like to know what I am missing and how to include moment in my .ts files in order to use it. I probably lack of some knowledge about the organisation of these modules, so every explanation will be welcome.
NodeJs version : 7.1.0
TypeScript version : 2.0.10
EDIT
After digging a bit, I found that I could compile my .ts files using tsc Test.ts --traceResolution to see if the imports are resolved correctly. Result : They are ! The real problem now is that my IDE (Visual Studio) doesn't know about moment.
The thing is that I have created each .ts file manually, by creating a new file, changing the extension and opening it in Visual Studio. Maybe that is the problem ? I'm just using Visual Studio as a text editor with IntelliSense. Do I have to make something in order to make Visual Studio understand that the import comes from NodeJS ?
You do not have to install moment.js interface because it provides its own types definitions.
Import moment.js in typescript (you were right).
import * as moment from 'moment';
Use moment.js in typescript
var momentObject: moment.Moment
momentObject.something() (ex: momentObject.add(..))

NPM alias node-postgres to node-postgres-pure

I'm building a nodejs application that needs to run on a Windows server. I've managed to get almost everything working, but I've ran into a problem with the postgresql driver. BrianC has a pure javascript implementation of the node-postgres that I would like to use to avoid having to build the postgres driver.
Is there a way for me to alias node-postgres(npm:pg) to node-postgres-pure(npm:pg.js) so that any dependent package that tries to install and resolve pg will install and resolve pg.js instead?
Thanks
Take a look at node-pg-pure-alias.
It's a bit of a hack, but the idea is to get a package that calls itself 'pg' installed into your node_modules folder. Then, that 'pg' library can export the "real" library.
See #it's not in npm! if you're concerned about depending on a package that's not in the official npm repository.
Disclaimer: I created node-pg-pure-alias. (All ten lines of the package.json, that is.)

Generation of svg on server side using highcharts

I did require('jsdom') in node js but i constantly get jsdom module not found.
More over i want to generate a svg document using highcharts on the server so that i can later on use that image in my pdf( I will convert that svg to image using batik).
Is there a link that may help me with this.
I have read through the http://blog.davidpadbury.com/2010/10/03/using-nodejs-to-render-js-charts-on-server/
but the prototype didnt make much sense. Is there a module which has been implemented rather than just a prototype.
You need to install all the required modules first. First download the tar file from his github https://github.com/davidpadbury/node-highcharts. Go to the directory where you saved the file and hit
npm install davidpadbury-node-highcharts-576f763.tar.gz
if you need to install additional modules like jsdom, try
npm install jsdom
His prototype seem to work fine with older version of highchart. but i am having some problem using it with latest highchart.
I hope this helped

Resources