简体   繁体   English

TypeScript:无法在模块外使用导入语句 (Node.js)

[英]TypeScript: Cannot use import statement outside a module (Node.js)

I've been developing a package in node using typescript.我一直在使用 typescript 在节点中开发 package。 When I install the package locally (ie providing the local path to the directory of the package, $ npm install../my_package ) everything works fine.当我在本地安装 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: 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错误消息图像

Try adding the line below to your package.json .尝试将下面的行添加到您的package.json

"type": "module",

If anyone is having the same problem while developing in typescript here's how I solved it.如果有人在 typescript 开发时遇到同样的问题,这就是我解决它的方法。

If you don't already have tsconfig.json file you can create one by $ tsc --init .如果您还没有 tsconfig.json 文件,您可以通过$ tsc --init创建一个。

Add the following to the tsconfig.json:将以下内容添加到 tsconfig.json:

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

In your package.json file add在您的 package.json 文件中添加

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

Compile the project with $ tsc .使用$ tsc编译项目。

Ps: in the .gitignore file add node_modules and /dist . ps:在.gitignore文件中添加node_modules/dist Now publish it using $npm publish .现在使用$npm publish发布它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取不能在 node.js 中的模块外使用导入语句 - Get Cannot use import statement outside a module in node.js Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js 不能使用 jest 在模块 node.js 之外使用 import 语句 - Cannot use import statement outside a module node.js using jest 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 带有 typescript 的 babel-node 在命令行中抛出“不能在模块外使用导入语句” - babel-node with typescript throws "Cannot use import statement outside a module" in command line 使用 ts-node 执行 typescript 代码会出现“无法在模块外使用导入语句”错误 - Executing typescript code using ts-node gives “Cannot use import statement outside a module” error SyntaxError: 不能在模块外使用 import 语句。 我正在使用 typescript 和 discord js - SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js 无法在后端使用 typescript 的模块外部使用导入语句 - Cannot use import statement outside a module using typescript on backend Typescript 与 mocha - 无法在模块外使用导入语句 - Typescript with mocha - Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM