ffflorian / node-packages

My Node.js packages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[mock-udp] Unable to import when using CommonJS

smnodame opened this issue · comments

I got this error from the latest version 1.4.1, I see you have cjs amd mjs folder which handle differrently for commonjs and es module
but the bundle file in cjs still using the import syntax which I suspect that it's wrong. the old version works (1.0.2) works fine for me since it compiles to use require syntax for import

/home/smnodame/MyProject/screenos-supervisor/node_modules/ntpclient/dist/cjs/index.js:2
import dgram from 'node:dgram';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require.extensions.<computed> [as .js] (/home/smnodame/MyProject/screenos-supervisor/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/smnodame/MyProject/screenos-supervisor/src/modules/datetime/dateTimeManager.ts:2:1)
[nodemon] app crashed - waiting for file changes before starting..

@smnodame Thanks for the report, I'll check it as soon as possible.

Hello :D
I got the same error again after running the jest test with ntpclient on 1.6.2
syntax on cjs still using the es6 syntax
I think on cjs folder, it suppose to use commonjs syntax (require instead of import)

Screenshot from 2024-01-10 16-34-53

Screenshot from 2024-01-10 16-37-03

I have the same problem: library does not work for my nodeJS applicaton - any solution to this ??

I am working with nestJS and typescript - but shouldn't matter, right ?

Here my error message:

$ nest start
/.../node_modules/ntpclient/dist/cjs/index.js:1
import * as dgram from 'dgram';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    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 Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (myapp.controller.ts:45:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.