user9164621
user9164621

Reputation:

How to use typescript modules in Node.js

It can seem banal for you but hopelessly I couldn't use one Typescript module (device-detector-js) in my Node.js project.

Now, Please know that I searched the web "How to use typescript modules in Node.js", all I find is "Building a Node.js App with TypeScript"; I think this is different, isn't it ?

Now, back to my case, installing device-detector-js then putting import DeviceDetector = require("device-detector-js"); yields this error:

SyntaxError: Cannot use import statement outside a module

Upvotes: 0

Views: 217

Answers (1)

P Varga
P Varga

Reputation: 20229

That's not a "TypeScript module", it's an npm package.

If your code is using CommonJS module format, then you can import a package with the syntax const DeviceDetector = require("device-detector-js").

If it's using EcmaScript module format (ESM), so not in your case, you can import with import * as DeviceDetector from "device-detector-js".

The syntax you are trying isn't valid in either case.

See here about how to tell Node that your code is using EcmaScript module format, if you wish to do that.

Upvotes: 1

Related Questions