[英]TypeScript: Cannot use import statement outside a module (Node.js)
我一直在使用 typescript 在節點中開發 package。 當我在本地安裝 package 時(即提供 package 目錄的本地路徑, $ npm install../my_package
)一切正常。
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
我在任何地方都找不到解決問題的方法。 如果有人可以在這里幫助我,我將不勝感激。
嘗試將下面的行添加到您的package.json
。
"type": "module",
如果有人在 typescript 開發時遇到同樣的問題,這就是我解決它的方法。
如果您還沒有 tsconfig.json 文件,您可以通過$ tsc --init
創建一個。
將以下內容添加到 tsconfig.json:
"declaration": true,
"outDir": "./dist",
"rootDir": "./",
在您的 package.json 文件中添加
"types": "dist/index.d.ts",
"main": "dist/index.js",
使用$ tsc
編譯項目。
ps:在.gitignore
文件中添加node_modules
和/dist
。 現在使用$npm publish
發布它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.