admin管理员组文章数量:1430368
Firstly, I am very to the world or Node.js etc so apologies if this is an obvious problem.
I am creating a custom packages with a few ponents:
One ponent for example is this?
class MailProcess {
constructor(name, code) {
this.name = name;
this.code = code;
}
}
module.exports = {MailProcess: MailProcess};
and in my index file:
export {MailProcess} from "./mail/MailProcess";
// export more ponents like above
and my json file:
{
"name": "packagename",
"version": "1.0.3",
"description": "Private package",
"main": "index.js",
"publishConfig": {
"registry": "/...."
},
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1172.0",
"mysql": "^2.18.1"
}
}
and in my Main code I (after installing the above package) I implement like this:
let {MailProcess} = require('@myusername/mypackagename');
but the error get thrown of
SyntaxError: Unexpected token 'export' relating to the index file.
I am wondering what I might be doing wrong. Essentially my pack contains a few different classes and I try to export them in index so the code using this package has access
Thanks for any help
Firstly, I am very to the world or Node.js etc so apologies if this is an obvious problem.
I am creating a custom packages with a few ponents:
One ponent for example is this?
class MailProcess {
constructor(name, code) {
this.name = name;
this.code = code;
}
}
module.exports = {MailProcess: MailProcess};
and in my index file:
export {MailProcess} from "./mail/MailProcess";
// export more ponents like above
and my json file:
{
"name": "packagename",
"version": "1.0.3",
"description": "Private package",
"main": "index.js",
"publishConfig": {
"registry": "https://npm.pkg.github./...."
},
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1172.0",
"mysql": "^2.18.1"
}
}
and in my Main code I (after installing the above package) I implement like this:
let {MailProcess} = require('@myusername/mypackagename');
but the error get thrown of
SyntaxError: Unexpected token 'export' relating to the index file.
I am wondering what I might be doing wrong. Essentially my pack contains a few different classes and I try to export them in index so the code using this package has access
Thanks for any help
Share Improve this question edited Jul 12, 2022 at 19:19 phuzi 13.1k4 gold badges29 silver badges59 bronze badges asked Jul 12, 2022 at 19:05 user19517065user19517065 431 gold badge1 silver badge7 bronze badges 5- I think you can do this instead : export * from "./mail/MailProcess"; – Colin Hale Commented Jul 12, 2022 at 19:11
- I don't think node js allows/reads export ponents. I have encountered this error too. – Alexander Obidiegwu Commented Jul 12, 2022 at 19:12
- @ColinHale unfortunately also doesn't work – user19517065 Commented Jul 12, 2022 at 19:27
- I guess i'm not sure but this may help. Looks like you may need to add the directory to the "exports" property in the package.json. nodejs/api/packages.html See: Package entry points – Colin Hale Commented Jul 12, 2022 at 19:33
-
Why are you mixing Commonjs
module.exports
with ESMexport
declarations? Decide on one style (preferably the modern ESM), then use it everywhere. Read nodejs/api/modules.html and nodejs/api/esm.html. – Bergi Commented Jul 12, 2022 at 19:37
2 Answers
Reset to default 2Just set the type property to module in the package.json file:
{
"name": "blabla",
// ...
"type": "module",
// ... rest
}
Ok so I had this same issue, albeit with a different setup and I stumbled upon this question. At this point it may not be needed anymore, but I wanted to share what solved the problem in my case, in case it may help somebody else down the line.
My setup was basically a NodeJs (v16) project acting as a server, a frontend (not relevant to the problem) and the shared Types package.
My issue was that while everything worked fine when using npm link
, it would not when publishing the Types package and installing it. Needless to say that the error was the same one:
export * from "./blocks";
^^^^^^
SyntaxError: Unexpected token 'export'
More specifically, in the top level index.ts
of the Types package, which was "exporting everything out".
To give an idea, this was the relevant part of my package.json
for my Types package:
"name": "vnotes-types",
"version": "1.1.3",
"description": "types for the VNotes application",
"main": "./src/index.ts",
"scripts": {
"pile": "tsc"
},
Moreover, on tsc
I was piling everything to a ./lib
folder.
And the solution, basically, was:
- Changing
"main": "./src/index.ts"
to"main": "./lib/index.js",
. Here the problem was that I would employ the non-piledindex.ts
(/src/index.ts
) and import everything from there. - Adding
"types": "./lib/index.d.ts"
. Basically points out to the type declarations. - I think this one isn't 100% necessary, but it reduces the size and clutter of the package:
"files": ["./lib"]
will only bundle the/lib
folder, so your/src
folder won't be included in the installation.
Hope this one helps people avoid wasting a stupid amount of time like I did to solve this issue.
本文标签:
版权声明:本文标题:javascript - NodeJS: SyntaxError: Unexpected token 'export' when trying to create a node module package - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745487245a2660436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论