1

I'm using SublimeText editor in combination with SublimeREPL which allows code snippets to be run from the text editor in different languages. I used this regularly with Python and now trying to use same with Node.js. I'm able to execute javascript from within the editor but have run into issue loading my own personal modules (local file). Below describes the issue.

I'm using SublimeREPL with node. I tried to load a module from a local file. The require command seems to run successfully but I get "not a function" when trying to run a function from within the module.

The same works fine when running from node command line.

Here is the contents of my primary js file:

console.log("Loading norm.js")
var norm=require("./norm.js")

console.log("Contents of norm module:")
console.log(norm)

// Now try to run a method from norm.js listed in exports
console.log("Output:")
let x=norm.normalizeAPI({ contact: {id:1, custom_field: {cf_country:'Canada'}}}, 'crm', 'contact')
console.log(x)

The module defines some functions and uses module.exports to export 3 of them.

When I run primary file from command line I get expected output:

> node test.js
Loading norm.js
Contents of norm module:
{
  normalizeAPI: [Function: normalizeAPI],
  normalizeBuiltins: [Function: normalizeBuiltins],
  normalizeCustomFields: [Function: normalizeCustomFields]
}
Output:
{ custom_fields: { cf_country: 'Canada' }, id: 1 }

But when I try to run same from within SublimeREPL, I get following output:

Loading norm.js
Contents of norm module:
{ }
Output:
Uncaught TypeError: norm.normalizeAPI is not a function

I use process.chdir to change to directory where files are located. Both the primary file and the module are in same directory. require() must be finding the file because if I change the path to invalid filename I get error. It seems to me the problem is either no objects are being loaded or the exports are not set.

Inside the module, I have tried to export using module.exports= and exports=.

Is it possible to load a local file module from within SublimeREPL and if so, how?

Steve
  • 33
  • 6
  • While SublimeREPL is a great tool (I use it daily with the Python REPL), it is essentially abandonware, and hasn't been updated in 8 years, so any bugs in it are not likely to be fixed. I'm not saying this is a bug in SublimeREPL, just giving you some background. As an alternative if you can't get this to work, check out [`Terminus`](https://packagecontrol.io/packages/Terminus). It requires a bit of setup, but it's not overly burdensome at all. It has many more features, and the author is still active in the Sublime plugin community. – MattDMo Feb 15 '23 at 13:44
  • @MattDMo, thanks for the suggestion. I looked over Terminus but can't see how it would give me the same ability to execute a single line at a time and hole state. But I did find a workaround to my issue. I'll describe that in an answer. – Steve Feb 20 '23 at 08:09

1 Answers1

0

After not getting response here or on the SublimeREPL github project (which as @MattDMo points out is a defunct project), I experimented with a workaround. I came up with the following which works pretty well.

So the initial goal was to run a single .js file that itself had references to my own personal modules in local files. Instead of using require, I go thru following process for each module:

  1. Run all functions and properties in the module in SublimeREPL as normal (select all and execute in SublimeREPL)

  2. create an object that is same name as what you store results of require into. E.g.:

    norm={}
  1. create objects inside norm for any functions or properties of the module in your exports list. E.g.:
    norm.normalizeAPI=normalizeAPI
    norm.normalizeBuiltins=normalizeBuiltins
    norm.normalizeCustomFields=normalizeCustomFields

Now you can run other functions in your primary script without having to edit them. E.g.

    norm.normalizeAPI(arg1, arg2)

To make it easy, I add a comment in the bottom of the module as follows:

    /*
    norm={} 
    norm.normalizeAPI=normalizeAPI
    norm.normalizeBuiltins=normalizeBuiltins
    norm.normalizeCustomFields=normalizeCustomFields
    */

Then I run this in SublimeREPL also.

Note: All functions and variables in the module are stored in the global namespace so name collision may be a problem with this approach but not for my use case.

Steve
  • 33
  • 6