#Hosting a school project via Render: Cannot use import statement outside a module.

23 messages · Page 1 of 1 (latest)

frozen crown
#

I've recently finished a college project using TypeScript and Node.js.

The final part of our assignment is to host this project on a cloud provider like Render (This was recommended by them).
I've followed all the steps they provided me with to set up the hosting of the project. But every time i try to run my build i get the following error:

import { Character } from './types';
^^^^^^
SyntaxError: Cannot use import statement outside a module

I've tried a few different solutions i found online such as adding Type: Module to my package.json, but this makes my entire project not recognise the .ts file extension.

The project works and runs perfectly by itself with no errors. It's only when i try to deploy it via Rendor where i'm having issues.

fossil marlin
#

It sounds like you are submitting TypeScript code, which is not understood by ordinary JavaScript runtimes. You need to compile the code with tsc and submit the JavaScript (.js files)

#

How are you running the project locally? tsx / ts-node?

frozen crown
#

Locally i'm running it with ts-node

fossil marlin
#

Yeah makes sense

#

TypeScript is not a programming language, it's a "meta-language", the type annotations we add in TS are just for typechecking, then it needs to be compiled down into JavaScript, which means removing those type annotations and any other TS elements.

#

A simple setup is something like
package.json:

  ...
  "script": {
    "build": "tsc",
    "start": "node dist/index.js"
  }
  ...
#

then you would run build, deploy to the hosting, and run start there

#

But there are a ton of variations on that.

#

render seems to suggest that you provide them with a build command, and let them build it on their platform, which is fine too.

frozen crown
#

Yeah i provide Render with a build and start command. The guide in our assignment gave us npm install and npm start as our build and start commands

fossil marlin
#

So you have 2 choices

#

You can build the code locally, save that compiled code to your git repository, then use npm install and npm start

#

Or you not build locally, and use npm install && npm build as your build command

#

You need to compile the typescript at some point

#

Or you could put install in your build command

#
 "scripts": {
    "build": "npm install && tsc",
    "start": "node dist/index.js"
  }
#

or use npm install && tsc

#

Just different ways to write the same thing

frozen crown
#

Thank you, i'll give some of these options a try

fossil marlin
#

No probs

#

I would suggest moving over to pnpm install of npm if you're permitted. It's pretty much the same but faster. npm install -g pnpm (You need need an adminstrator access command prompt on windows or sudo on linux).