I have a node.js project using webpack that's being deployed to AWS Lambda. However, I also have a single python file in the project that's powering one of the Lambda functions (i ported a legacy python script over to lambda for automation purposes). In my serverless.yml, I've set the appropriate runtimes (node.js/python) for the functions.
My problem is that webpack throws errors about my python file (it's even referencing a python/handler.js file that doesnt exist - the python handler is python/handler.py).
The exact error is:
ERROR in ./python/handler.py 2:0
Module parse failed: Unexpected token (2:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
I just want webpack to bundle the regular .js files, and leave the python file alone. Note that deploying the project without webpack works, and gives me lambda functions powered by javascript and python. Does anyone have any suggestions to my issue?
Simply add **/*.py to your excludeFiles block.
custom:
webpack:
excludeFiles: **/*.py # Provide a glob for files to ignore
You can read more in the documentation
Side note - you may consider trying es-build instead, which is often 10x faster than webpack. You can read more here
Related
I have a simple javascript function in an example.js file placed inside the node js public directory.
I am using jest for unit testing.
The problem is if I write the following in the example.js javascript file in the public folder:
module.exports.myFunction = myFunction;
Jest test file is able to import it, using require() and perform tests, however when I run the web application, the browser complains when I service the page containing this javascript:
Uncaught ReferenceError: module is not defined
What is the correct way to test javascript files in the public directory of the node application?
Using export and/or import in the project is reported an being unrecognized and results in errors as well.
How is this done?
As mentioned in comments require(...) and module.exports relate to Common JS Modules, which are natively supported by NodeJS runtime, but not by browser. So basically you'll need to add extra build configuration to have your module work in both runtimes.
If you want to have outputs in both CommonJs and Browser friendly bundle - you can write all code in ES Modules and use build tools like webpack to provide outputs in different formats.
Also, starting from Node 13.2.0 - it supports ES modules natively. So I would stick to ES modules for ongoing development anyway.
Please also check this short article on main JS module format differences.
I'm trying to use a plugin called simplemde I used bower to bring in the plug in. The javascript it imported is trying to require other addons but when I try to run the code I get this error in the console Uncaught ReferenceError: require is not defined. I understand the you cannot require from the client side unless you make changes to the asset pipeline. I'm trying to find out what to do in this situation with a rails app.
JS
var CodeMirror = require("codemirror");
require("codemirror/addon/edit/continuelist.js");
require("./codemirror/tablist");
require("codemirror/addon/display/fullscreen.js");
require("codemirror/mode/markdown/markdown.js");
require("codemirror/addon/mode/overlay.js");
require("codemirror/addon/display/placeholder.js");
require("codemirror/addon/selection/mark-selection.js");
require("codemirror/mode/gfm/gfm.js");
require("codemirror/mode/xml/xml.js");
What are some ways I can get this file to work with Rails?
I bet you use files from the src directory of the SimpleMDE plugin. This files should be preprocessed and assembled into one on the server side before delivering to the client. You can use minified and assembled files from the dist directory. If you want to use source files anyway, you should tune your asset pipeline, for example like in article Bring CommonJS to your asset pipeline or use other tools, like Grunt or Gulp, for assets assembling.
I have copied the libjpeg library into the 'c' directory where I am keeping the C and C++ source code for my node.js application.
I have used the binding.gyp file to point to the C++ file, and it has built successfully. However, when I add the line
#include "libjpeg-master/jpeglib.h"
I get the error message
fatal error C1083: Cannot open include file: 'jconfig.h': No such file or directory [...]
I am attempting to build the libjpeg library alongside the node.js code, rather than first building libjpeg using the build configuration there.
How should I go about solving this. If the answer is to use the build configuration from libjpeg, how can I get that to work when building a node.js plugin?
I am currently developing this on Windows but I want to get this working properly for all platforms.
I am trying to get Swig (the template language) working on Parse Cloud Code with Express. Parse Cloud Code is a Node/Express host that doesn't allow NPM. Ridiculous, I know. I can still load external files into code with requires statements however, so I think that there's hope I can get this working.
So my question is how do I get the whole entire Swig package into a single JS file that I can include from my Parse Express app like so:
var swig = require("./cloud/swig.js");
Worth noting that Parse breaks normal require statements so that the NPM package as-is doesn't work without modifying each and every single file in the node_modules folder to have cloud in its path (which is why my above path has cloud in it). Parse also chokes while uploading lots of small files. Concatenation is a need on this platform.
I have tried playing with browserify for hours, but no combination of anything I do makes exposes the Swig object when I load the browserified file with the require statement. I think it may be the right option since the Browserified file includes all the files from Swig, but it doesn't expose them externally.
My question is either can this be done in browserify, and if so, how? Or is there another way to concatenate a NPM repo down to one file so it can be more easily included from this platform?
Thanks so much.
Browserify is not the right tool for the job.
As the name implies, browserify is intended to be used to generate files you want to execute in the browser. It walks the require calls from an entrypoint (i.e. some JS file you pass to browserify) and bundles them in an object that maps their names to functions wrapping the modules. It does not expect a require function to already exist and doesn't make any use of it. It substitutes its own implementation of require that only does one thing: look up names from the bundle, execute the matching function and return its exports.
You could theoretically require a browserify bundle, but it would just return an empty object (although it might mess with globals). And in all likelihood it might break because the bundled modules think they are being executed in a browser. This won't do any good.
The only sane option if you want to stick with the host, is to copy over the node_modules folder from your local project folder. This may not work if your computer and the server are not 100% compatible (e.g. 32-bit vs 64-bit, Debian vs RedHat, OSX/Windows vs Linux) but this mostly depends on your exact dependencies (basically anything that is built with node-gyp can be a problem).
Node.js uses the node_modules folder when looking up dependencies in require calls automagically. If you can somehow get a node_modules folder with the right contents on the server, require("foo") will work as long as node_modules contains a module foo.
Ultimately, you are trying to use npm modules in Parse Cloud code and currently it's not possible:
https://parse.com/questions/using-npm-modules-in-cloud-code
But if you are only trying to use Swig, then as a work-around, you can consider using underscore template instead. Parse already includes underscore:
https://parse.com/docs/cloud_modules_guide#underscore
I'd like to start bundling our javascript files. I've found that it's really easy locally using the web essentials plugin, however I need to set up the build server to generate the bundled .js file.
I'd rather not check this generated file into TFS as it will cause conflicts for our developers, and also since it's generated from the source I feel that the server build should generate it.
Is there a command line utility for doing the script bundling outside of visual studio that could be used as part of a build script? My google-fu is failing to find one.
Many thanks,
As long as you wrote it as proper AMD modules, require.js comes with a tool to turn all your files into an optimized bundle.