0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

While the syntax is indeed wrong, doesn't the error arise already from the import usage itself?
@aviya.developer, yes, it does, that import should just be const (or var for very old Node versions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.