Include local file


#1

I have some library files in my computer. How can I use these file in my project?
My code as show below, and failed.
@include once “lib/RPC/RPC.class.nut”


#2

You should use Builder, which is a squirrel preprocessor that understands the @include syntax. See https://github.com/electricimp/Builder.


#3

I used the example code:
@include “github:electricimp/Promise/promise.class.nut”
However this code cannot run as well. So I don’t know how can I go ahead.


#4

Are you using builder? Can you paste the error message you see?


#5

@include statements will not work in the IDE. To include libraries in the IDE, use #require. See https://developer.electricimp.com/codelibraries for more details.

Builder is intended to be run locally in order to preprocess your Squirrel files, and you then upload your files with the impCentral API.

It requires node.js. You install Builder with npm install -g Builder. Then you can use it to preprocess files with:

 pleasebuild example.agent.nut >example.agent.nut_

Then you need to upload the output (I’ve used a .nut_ extension) to impCentral. If you’re experimenting, you can just copy and paste it into the IDE. If you want something more “production”, you can use impTool: https://github.com/electricimp/imp-central-impt.

I just tried the Promise example locally, and it works fine.


#6

So, you means that I need to build some new libraries, and upload to the impCentral, then use #require to load these class. However in my demo code, they only use @include once …,
Such as: @include once “lib/RPC/RPC.class.nut”
and the error information is : “there’s a problem with your code. Fix it and try again.


#7

As mentioned above, to use @include you will need to use Builder, which is a tool you run on your own computer.

You cannot use @include with the web-based impCentral IDE. However, you can copy of the contents of your library file and paste it into the main code in the impCentral windows. But Builder is better, especially as you add more local files to your code.