2

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?

Community
  • 1
  • 1
Dan L
  • 3,896
  • 3
  • 30
  • 63
  • I think you've got your technologies mixed up Dan. You've installed `momentjs` through Node.js so it can only be used in Node.js. Is your aim to make a server-side snippet for use in Node.js or a client-side snippet for use in the browser? Also, Atom is just an editor, it helps you edit files which you must create - it's not going to help you `require` moment which in `nodejs` would be done with: `var moment = require('moment');` – Jasdeep Khalsa Sep 05 '14 at 14:06
  • You're probably right. I heard Atom was built using node and since Atom is a web-based app (using Chromium) written in CoffeeScript, I thought it would be fairly simple to load an external JavaScript library and use it. I know Atom can create packages that have node libraries as dependencies, but that seems a little overkill when all I want to do is load and use a single JS library. The goal is just to create a snippet in Atom that puts out a nicely-formatted date while I develop. I can use it with raw JS, but it's ugly and not a good solution. Just want to clean it up a bit. – Dan L Sep 05 '14 at 14:19
  • I see! Have a look at my answer on your other question. Yes, I agree that an Atom package might be overkill for this, a simple webpage you have open on the side should do the trick! – Jasdeep Khalsa Sep 05 '14 at 14:47

2 Answers2

1

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

0

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.

nwinkler
  • 46,078
  • 18
  • 137
  • 157
  • Unfortunately, I'm beginning to think my own package is the only solution at the moment. I would have thought that since Atom is built using web tech, that I could have found a way to point to an external JS library, but that just doesn't seem to be the case. I'll check into my own package and accept this answer if I can't find a better solution. – Dan L Sep 07 '14 at 16:25