0

I've been developing a package in node using typescript. When I install the package locally (i.e. providing the local path to the directory of the package, $ npm install ../my_package) everything works fine.

But I've published the package in npm and now when I install the same package from npm it shows me the above-mentioned error:

Cannot use import statement outside a module

I could not find a solution to my problem anywhere. I would really appreciate it if anyone could help me out here.

Error Msg Image

0

2 Answers 2

1

Try adding the line below to your package.json.

"type": "module",
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Iman. But adding "type": "module", to my package.json doesn't actual solve my problem. Anyway, I found the solution myself. If anyone is having the same problem while developing in typescript here's how I solved it. Add "declaration": true, in tsconfig.json file (if you don't already have tsconfig.json file you can create one by $ tsc --init) also "outDir": "./dist", "rootDir": "./", . And in your package.json file add "types": "dist/index.d.ts", "main": "dist/index.js", .Compile the project by $ tsc . Ps.gitignore file add node_modules , /dist . Now publish it using $npm publish.
Post your answer in response to the question. It will help future people find the answer easier!
0

If anyone is having the same problem while developing in typescript here's how I solved it.

If you don't already have tsconfig.json file you can create one by $ tsc --init.

Add the following to the tsconfig.json:

"declaration": true,
"outDir": "./dist",
"rootDir": "./",

In your package.json file add

"types": "dist/index.d.ts",
"main": "dist/index.js",

Compile the project with $ tsc.

Ps: in the .gitignore file add node_modules and /dist. Now publish it using $npm publish.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.