Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example not working #5

Closed
sandro-pasquali opened this issue Dec 18, 2021 · 6 comments
Closed

Example not working #5

sandro-pasquali opened this issue Dec 18, 2021 · 6 comments

Comments

@sandro-pasquali
Copy link

Version: 1.0.2

Details

Example fails with TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".marko" for <path to template.marko>

Expected Behavior

rendered Marko when calling server

Your Environment

  • Node 17.2.0
  • OSX M1 Monterey

Steps to Reproduce

Run the example in README, in the above environment.

@DylanPiercey
Copy link
Contributor

I believe the issue is that you haven't configured the environment to be able to load Marko templates.

Marko is not understood by node or any environment by default and has to be preprocessed. In node you can use the Marko require hook require("@marko/compiler/register"), but we currently recommend bundling the node server for the best experience. You can see this in many of our boilerplate/example repos with webpack/rollup/vite https://github.com/marko-js/examples/.

I added a note to the docs here about this as well.

@mikko-severo
Copy link

Hi,

Beginner here, can I use "import "@marko/compiler/register" " to use the Marko required hook?

@DylanPiercey
Copy link
Contributor

DylanPiercey commented Sep 23, 2022

@mikko-severo we highly recommend avoiding using the Marko compiler hook. Marko is designed to be used with a bundler since one of the points of Marko is to be able to load just the JS needed for the client.

I'd recommend starting from one of our Vite template projects, eg

npm init marko -- --template vite-fastify

@mikko-severo
Copy link

@DylanPiercey Thanks for the insight and fast reply. I mainly needed Marko for the server rending (no need for a bundler, I think), but I couldn't just get it to work. I keep getting the error the OP posted when using Fastify w/ESM or CMJ syntax. Although I got it working with Express w/ CMJ syntax by requiring the "Marko/compiler/register."

Any idea how I can get it to work with Fastify for server rendering?

Thanks

@DylanPiercey
Copy link
Contributor

DylanPiercey commented Sep 26, 2022

@mikko-severo I still think even if you just want to use Marko for the server you should use a bundler. Here is vite express example which only bundles the server side https://github.com/DylanPiercey/marko-vite-express-ssr-only

Benefit here is that in prod your server startup doesn't need to go through the Marko compiler, and in dev you get cool things like hot reloading for your server code.

It also has an incremental path to using the client side bundler when/if you want to.

@mikko-severo
Copy link

mikko-severo commented Oct 4, 2022

Hi @DylanPiercey
Thanks. I got it to work w/common js.

`require('@marko/compiler/register');
const fastify = require('fastify')({
logger: false
}),
template=require( "./template.marko"),
// markoPlugin =require("@marko/fastify") ;

// fastify.register(markoPlugin);
// Declare a route
fastify.get('/', function (request, reply) {
let render = template.default.renderToString({}),
res_body = {"view": render };
//return res_body
reply.send(res_body);
})

// Run the server!
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants