There are two kinds of
tstl libraries published on npm:
- Type declaration libraries - Provides only ambient types. In other words, these libraries do not contain any code which can be executed.
- Lua libraries - Provides Lua code that can be imported and executed by
This page describes how to create a Lua package and publish it to npm.
.tsxsource files from a
tstlLua libraries can not be bundled with the
luaBundlecompiler flag. (The end-users consuming the library will decide whether they want the final product to use
tsconfig.json file must include the following fields:
package.json file should include the following fields:
// An array containing the files that will be published to npm. (See more information below.)
"dist/**/*.lua", // Only specify this if your library is a Lua library.
// Only specify "main" if your library is a Lua library.
// (Do NOT include the file extension here, or things will not work properly.)
There are many other fields that should be in a proper
package.json file, such as
version, and so on. Use
npm init to generate a new
package.json with some basic fields, if necessary.
- Regardless of the contents of the
filesfield, some files will always be published, like
- Modules specified in
"devDependencies"will not be available to the module at runtime.
- There is no need to publish the
tsconfig.jsonfile, as it will do nothing for the users of your module.
When you are ready to publish:
npm publish --dry-runto see what files would be published without actually uploading anything.
npm publishto actually upload it.
Using the Module
For an example of a Lua package published to npm, see
You can also reference the projects used in the TypeScriptToLua tests:
A project using dependencies from its node_modules directory with Lua code. These example dependencies include:
lua-global-with-decls: Lua code + TypeScript declarations defining global functions.
lua-global-without-decls: Lua code defining global functions.
- Declaration file is added manually in lua-global-without-decls.d.ts in the project sources.
lua-module-with-decls: Lua code + TypeScript declarations for 'module' files, i.e Lua files that return a table of exported functions.
lua-module-with-decls: Lua code for 'module' files, i.e Lua files that return a table of exported functions.
- Declaration files are added manually in lua-module-without-decls.d.ts in the project sources.
This project includes Lua files as part of the project's source files. To use the Lua from the files you have to provide declaration files with a matching name and location for each file. For examples
some_dir/library.d.ts. The declaration files contain the TypeScript declarations of the corresponding Lua file. Both Lua and .d.ts files should be checked into your repository!
This project contains two Lua source files:
luafile.lua: Some Lua right next to the .ts files using it.
lua_sources/otherluaFile.lua: Lua in a separate
lua_sourcesdirectory, in case you want to group all your Lua files into one directory.