Trails.js Web Application Framework
Trails.js is a modern, community-driven web application framework for Node.js, currently at version 3.2.2. It emphasizes a convention-based, API-driven design philosophy, inspired by frameworks like Ruby on Rails and Grails. Trails.js accelerates development through scaffolding provided by Yeoman and extends its capabilities via 'Trailpacks'—modular extensions that integrate existing ecosystem tools like Hapi, Express, Waterline, or Knex. The framework supports microservices architectures and is designed to work across Windows, Mac, and Linux, requiring Node.js 7.6.0 or newer. Its release cadence is stable, with major version updates occurring less frequently, focusing on stability and core enhancements.
Common errors
-
Error: Cannot find module 'trails'
cause The `trails` package is not installed as a dependency in your project or the global `npm install -g trails` was not followed by `yo trails` to generate an application.fixEnsure `trails` is listed in your project's `package.json` and run `npm install`, or, for a new project, use `npm install -g yo generator-trails` followed by `yo trails` to scaffold an application. -
TypeError: TrailsApp is not a constructor
cause Attempting to use ES Modules `import` syntax (`import TrailsApp from 'trails'`) with the `trails` package, which is primarily a CommonJS module.fixUse CommonJS `require` syntax: `const TrailsApp = require('trails')` to correctly import the TrailsApp class. -
ReferenceError: Controller is not defined
cause This error typically occurs when a custom controller file extends `Controller` but is either not being loaded by the Trails.js framework or the `Controller` base class is not explicitly imported in a non-framework context.fixEnsure your controller file is located in the `api/controllers` directory of your Trails.js application. If using outside the framework's discovery mechanism, explicitly import `const { Controller } = require('trails')`.
Warnings
- breaking With Trails v2.0, built-in Trails `Error` objects and resource classes (`Model`, `Controller`, `Service`, `Policy`) became globally accessible within the framework context. This changed how these core components were managed and accessed.
- deprecated The `trailpack-core` module was deprecated in v2.0, with its functionality merged directly into the Trails core. Installing `trailpack-core@2.0` results in a no-op.
- deprecated Individual modules like `trails-model`, `trails-service`, `trails-policy`, and `trails-controller` were merged into Trails core in v2.0 and deprecated as standalone packages.
- gotcha Trails.js requires Node.js version 7.6.0 or higher. Running with older Node.js versions will lead to syntax errors or unexpected behavior due to unsupported language features.
Install
-
npm install trails -
yarn add trails -
pnpm add trails
Imports
- TrailsApp
import TrailsApp from 'trails'
const TrailsApp = require('trails') - Controller
class MyController extends Controller { ... }const { Controller } = require('trails') - Service
class MyService extends Service { ... }const { Service } = require('trails')
Quickstart
#!/usr/bin/env sh
# First, install the global tools needed to scaffold a Trails app
npm install -g yo generator-trails
# Create a new Trails project (choose a 'web' or 'api' template)
yo trails my-trails-app
cd my-trails-app
# Example of a simple controller (api/controllers/HelloController.js)
# Note: `Controller` is implicitly available via the framework context.
printf "'use strict'\n\nmodule.exports = class HelloController extends Controller {\n greet (request, reply) {\n reply.send('Hello from Trails.js!')\n }\n}" > api/controllers/HelloController.js
# Example of adding a route (config/routes.js)
# Add this route to the `module.exports.routes` object in config/routes.js
# {\n# '/hello': {\n# handler: 'HelloController.greet',\n# method: 'GET'\n# }\n# }
# Now, run your Trails application
node server.js
# Open your browser or use curl to visit http://localhost:3000/hello