skywalking icon indicating copy to clipboard operation
skywalking copied to clipboard

[Bug] Agent.start is not a function when I use esm syntax

Open Jiannan-dev opened this issue 3 years ago • 5 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Apache SkyWalking Component

NodeJS Server Side Agent (apache/skywalking-nodejs)

What happened

When I use ESM syntax in node, it directly throws out agent.start is not a function ,I just copied the usage from Readme like this

import agent from 'skywalking-backend-js';
agent.start();

But when i use cjs synatax

const { default: agent } = require('skywalking-backend-js');

agent.start() 

it will work fine

I can reproduce it in node v16 and node v14,lower versions I have not tried

What you expected to happen

work fine

How to reproduce

npm install skywalking-backend-js

and write follow to test.js

import agent from 'skywalking-backend-js';
agent.start();
node test.js

Anything else

No response

Are you willing to submit PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

Jiannan-dev avatar Mar 08 '22 13:03 Jiannan-dev

Do we need some other packaging tool (like rollup) to package the chunks in cjs and esm formats separately, and then differentiate them in package.json, like this

main: 'index.cjs'
module: 'index.mjs'

This allows the runtime environment to determine whether to load index.cjs or index.mjs depending on the syntax used

Jiannan-dev avatar Mar 08 '22 13:03 Jiannan-dev

Do we need some other packaging tool (like rollup) to package the chunks in cjs and esm formats separately, and then differentiate them in package.json, like this

main: 'index.cjs'
module: 'index.mjs'

This allows the runtime environment to determine whether to load index.cjs or index.mjs depending on the syntax used

This sounds good to me, are you willing to open a pull request? @ruleeeer

kezhenxu94 avatar Mar 08 '22 14:03 kezhenxu94

Do we need some other packaging tool (like rollup) to package the chunks in cjs and esm formats separately, and then differentiate them in package.json, like this

main: 'index.cjs'
module: 'index.mjs'

This allows the runtime environment to determine whether to load index.cjs or index.mjs depending on the syntax used

This sounds good to me, are you willing to open a pull request? @ruleeeer

@kezhenxu94 No problem, I'll try to use rollup packaging

Jiannan-dev avatar Mar 08 '22 14:03 Jiannan-dev

Any update?

wu-sheng avatar Apr 01 '22 06:04 wu-sheng

Any update?

No update .I've been a little busy lately.

Jiannan-dev avatar Apr 01 '22 12:04 Jiannan-dev

I also faced this issue, so I do this to bypass it first

import agent from 'skywalking-backend-js'

 const agentInstance = has('default', agent)
      ? (agent.default as typeof agent)
      : agent

    agentInstance.start()

marklai1998 avatar Oct 21 '22 06:10 marklai1998

@kezhenxu94 @ruleeeer Would you fix this? It seems no update for months

wu-sheng avatar Dec 01 '22 10:12 wu-sheng