Package import issue in ESmodule app
Closed this issue · 2 comments
First of all, thanks for create this package =)
I'm beginner programmer of javascript.
So maybe not issue in this package...
My project is in service on ESmodule
I wanna use the class "TSIDGenerator"
- So I imported like
import { TSIDGenerator } from 'tsid-ts';
but I confront error
======
/packages/tsid/lib/tsid.js:4
import { TSIDGenerator } from 'tsid-ts';
^^^^^^^^^^^^^
SyntaxError: Named export 'TSIDGenerator' not found. The requested module 'tsid-ts' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'tsid-ts';
const { TSIDGenerator } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
Node.js v18.17.0
- So I fixed my code as guidance
import pkg from 'tsid-ts'; const { TSIDGenerator } = pkg;
but this time is I have another problem like
=====
(node:787102) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ...
to show where the warning was created)
Waiting for the debugger to disconnect...
/node_modules/.pnpm/tsid-ts@0.0.6/node_modules/tsid-ts/dist/index.js:1
export * from './tsid';
^^^^^^
SyntaxError: Unexpected token 'export'
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at ModuleWrap. (node:internal/modules/esm/translators:169:29)
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v18.17.0
======
My project already use "type": "module"
- I couldn't understand that, so I tried to use to this package, several methods.
but I can't find to use this package without edit "tsid-ts"
- I can use this package by editing like this,
first line is import { decode, encode, uniformInt } from './utils';
of dist/tsid.js
I just added ".js" like import { decode, encode, uniformInt } from './utils**.js**';
and I import like import { TSIDGenerator } from 'tsid-ts/dist/tsid.js';
- I wanna use this pacakge without edit, just Warts and all
what can I do?
Hi @LeoniceHan,
The tsid-ts
library was originally developed for TypeScript projects, but I've made some adjustments, and now it can also be used for regular JavaScript project as well.
Please upgrade tsid-ts
to version 0.0.8
or above.
if the type
of your JavaScript project is module
import { TSIDGenerator } from "tsid-ts";
const tsidGenerator = new TSIDGenerator();
const tsid = tsidGenerator.create();
console.log(tsid.toString());
if the type
of your JavaScript project is commonjs
const { TSIDGenerator } = require("tsid-ts");
const tsidGenerator = new TSIDGenerator();
const tsid = tsidGenerator.create();
console.log(tsid.toString());
It works well!
Thanks yubin, I appreciate it.