Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the “License”); you may not use this file except in compliance with

the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an “AS IS” BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

layout: docpage title: Load external Node.js modules description: Require Node.js modules installed from npm in ActionScript and Apache Royale permalink: /features/nodejs/external-modules

Load external Node.js modules with Apache Royale

Expose Node.js modules installed from npm to ActionScript

By creating type definitions with special metadata, developers can use Node.js modules from ActionScript code.

Let's try to use the boxen module from npm in ActionScript.

Create a file named boxen.as and add the following code:

package
{
	/**
	 * @externs
	 */
	[JSModule(name="boxen")]
	public function boxen(message:String, options:Object = null):void {}
}

This file will tell the Apache Royale compiler how to access the “boxen” module in the generated JavaScript. Let's look in more detail at each part of this file.

All type definitions must include the @externs asdoc tag.

/**
 * @externs
 */

Additionally, type definitions for Node.js modules must include [JSModule] metadata.

[JSModule(name="boxen")]

Set the name field inside [JSModule] to the string that should be passed to a require() call in JavaScript to load the module.

The default export of a module should either be a class, a function, or a variable. In this case, the default export of the “boxen” module is a function.

public function boxen(message:String, options:Object = null):void {}

The name of the default export should be derived from the name of the module. In this case, the name of the module and the name of the function are both boxen.

If the module name contains dashes, the symbol name should be converted to camel case. For example, [JSModule(name="my-example-module")] would require the symbol to be named myExampleModule.

Use the module

Create a file named MyScript.as, and add the following content:

package
{
	public class MyScript
	{
		public function MyScript()
		{
			boxen("I loaded a module!");
		}
	}
}

Compile and run

Using Apache Royale's asnodec compiler, compile our project into JavaScript that can be run with Node.js:

asnodec MyScript.as

Use the following command to run the compiled project:

node bin/js-release/index.js

The following output will appear in the console:

┌────────────────────────┐
│                        │
│   I loaded a module!   │
│                        │
└────────────────────────┘