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?
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:
Run all functions and properties in the module in SublimeREPL as normal (select all and execute in SublimeREPL)
create an object that is same name as what you store results of require into. E.g.:
norm={}
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.
Related
I'm creating a NodeJS service with serverless framework to validate a feed so I added a schema file (.json) to the service but I can’t make it work.
It seems to not be included in the package. Lambda doesn't find that file.
First I just run sls package and check the zip contents but the file is not present.
I also tried to include the file with:
package:
include:
- libs/schemas/schema.json
but still not works.
How can I make sure a static file is included in the package and can be read inside the lambda function?
It depends on how you are actually trying to load the file.
Are you loading it with fs.readFile or fs.readFileSync?
In that case, Serverless doesn't know that you're going to need it. If you add a hint for Serverless to include that file in the bundle then make sure that you know where it is relative to your current working directory or your __dirname.
Are you loading it with require()? (Do you know that you can use require() to load JSON files?) In that case, Serverless should know that you need it, but you still need to make sure that you got the path right.
If all else fails then you can always use a dirty hack.
For example if you have a file 'x.json' that contains:
{
"a": 1,
"b": 2
}
then you can change it to an x.js file that contains:
module.exports = {
"a": 1,
"b": 2
};
that you can load just like any other .js file, but it's a hack.
From what I've found you can do this in many ways:
As it is stated in another answer: if you are using webpack you need to use a webpack plugin to include files in the lambda zip file
If you are not using webpack, you can use serverless package commnad (include/exclude).
You can create a layer and reference it from the lambda (the file will be in /opt/<layer_name>. Take in consideration that as today (Nov20) I haven't found a way of doing this if you are using serverless.ts without publishing the layer first (lambda's layer property is an ARN string and requires the layer version).
If your worried about security you can use AWS Secrets as it is stated in this answer.
You can do what #rsp says and include it in your code.
For those struggling to find a solution in 2022: use package.patterns parameter. Example:
package:
patterns:
- libs/schemas/schema.json
- !libs/schemas/schema-prod.json
(! in front of the file path excludes specified pattern)
Documentation: https://www.serverless.com/framework/docs/providers/aws/guide/packaging
A bit about what I'm trying to achieve.
I'm building dev library that shows the list of files. And I want to set file color depending on when file was changed.
So, as a result of generation, I want an array like this:
[
{
lastChange: '2009-06-29T11:11:55Z',
fileContents: {name: 'VmSome'},
},
// ...
]
This is meant to work in browser environment. Meaning all file related information should be included into bundle.
Current progress
At the moment I'm not quite sure whether that's even possible.
I'm getting a list of files via webpack require.context:
require.context('./tree', true, /.js$/)
This gives me access to file content and path. But not to anything else.
Thanks for your attention.
I would first try modifying webpack-file-list-plugin. As of now, it creates a JSON that gives all files packed, their name and some more info... You could definitely add your code to fetch more information to the JSON.
https://github.com/object88/webpack-file-list-plugin/blob/master/src/index.js
How can I detect the entry file in Node.js?
For example, if I launch my application by typing node file1.js, how can I tell that file1.js was the entry point?
You can also use require.main.
Accessing the main module
When a file is run directly from Node,
require.main is set to its module. That means that you can determine
whether a file has been run directly by testing
require.main === module
For a file foo.js, this will be true if run
via node foo.js, but false if run by require('./foo').
Because module provides a filename property (normally equivalent to
__filename), the entry point of the current application can be obtained by checking require.main.filename.
hot sauce
process.argv[1] provides that information.
I am working with minko and managed to compile MINKO SDK properly for 3 platforms (Linux, Android, HTML5) and build all tutorials / examples. Moving on to create my own project, I followed the instructions on how to use the existing skeleton project, then using an existing example project.
(I believe there is an error in the skeleton code at this line :
auto sceneManager = SceneManager::create(canvas->context()); //does not compile
where as the example file look like this :
auto sceneManager = SceneManager::create(canvas); //compile and generate binary
I was able to do so by modifying premake5.lua (to include more plugins) and calling script/solution_gmake_gcc.sh
to generate the make solution a week ago. Today, I tried to make a new project in a new folder but calling
script/solution_gmake_gcc.sh and script/clean failed with this error:
minko-master/skel_tut/mycode/premake5.lua:3: attempt to index global 'minko' (a nil value)
Now at premake5.lua line 3 there is this line : minko.project.solution(PROJECT_NAME),
however sine i am not familiar with lua at all, can anyone shed any light on the issue ?
What is supposed to be declared here, why is it failing suddenly... ?
(I can still modify,compile and run the code but i can't for example add more plug-ins)
PS: weirdly enough, the previously 'working' project is also failing at this point.
Thanks.
PROJECT_NAME = path.getname(os.getcwd())
minko.project.application("minko-tutorial-" .. PROJECT_NAME)
files { "src/**.cpp", "src/**.hpp", "asset/**" }
includedirs { "src" }
-- plugins
minko.plugin.enable("sdl")
minko.plugin.enable("assimp")
minko.plugin.enable("jpeg")
minko.plugin.enable("bullet")
minko.plugin.enable("png")
--html overlay
minko.plugin.enable("html-overlay")
Assuming that's indeed your project premake5.lua file (please us the code tags next time), you should have include "script" at the beginning of the file:
https://github.com/aerys/minko/blob/master/skeleton/premake5.lua#L1
If you don't have this line, it will not include script/premake5.lua which is in charge of including the SDK build system files that defines everything inside the minko Lua namespace/table. That's why you get that error.
I think you copy pasted one of the examples/tutorials premake5.lua file instead of modifying the one provided by the skeleton. The premake conf file of the examples/tutorials are different since they are included from the SDK premake files. But your app premake5.lua does the "opposite": it includes the SDK conf files rather than being included by them.
The best practice is to edit your app's copy of the skeleton's premake5.lua (instead of copy/pasting one from the examples/tutorials).
(I believe there is an error in the skeleton code at this line :
That's possible. Our build server doesn't test the skeleton code. That's a mistake we will fix ASAP to make sure it works properly.
script/solution_gmake_gcc.sh and script/clean failed with this error:
minko-master/skel_tut/mycode/premake5.lua:3: attempt to index global 'minko' (a nil value)
Could you copy/paste your premake5.lua file?
Also, what's the value you set for the MINKO_HOME env var? Maybe you've moved the SDK...
Note that instead of setting a global MINKO_HOME env var, you can also set the corresponding LUA constant at the very begining of your premake5.lua file.
I am new here and pretty new to Node.js. I got Express working fine, connecting to MySQL (database) is going fine, and socket io is working fine.
But I decided to split many of these features up in separated files. To keep my main JS file nice and clean. I made it possible to get variables from other js files back to my main.js script. Either using exports, or global. I find global working easier since most of them are functions. It's all working fine to this point.
But now the issue that I am having. I'm loading 3 js files in my main.js file. I am requiring the first js file, I call the function that is in that js file and store the result in a variable. That's going fine. But now the second js file is suppose to use or grab this variable, and that isn't working.
My question is, how do I make that work?
It is a matter of your design.
You should use module.exports to return a variable from a file.
Example:
file1.js
function someFunction() {return true;}
module.exports = someFunction();
main.js
console.log(require('./file.js')); // true
So, if the second file depends on the variable from the first file, you may also require the file1.js in the second one.
Or, export a function that accept one parameter in the second file. The main file should then use the variable from first file to call the function.