locatorjs icon indicating copy to clipboard operation
locatorjs copied to clipboard

bug: SyntaxError: Cannot use import statement outside a module

Open yf-hk opened this issue 1 year ago • 4 comments

Where the bug happens

  • [ ] Browser Extension
  • [X] Library
  • [ ] Other

your browser

No response

Describe the bug

When using @locator/runtime:

import setupLocatorUI from '@locator/runtime/dist'

if (process.env.NODE_ENV === 'development') {
  setupLocatorUI()
}

It prompts this error message:

\node_modules\.pnpm\@[email protected][email protected]\node_modules\@locator\runtime\dist\index.js:1
import { initRuntime } from "./initRuntime";
^^^^^^

SyntaxError: Cannot use import statement outside a module

To reproduce

  1. Install Next
  2. Install @locator/runtime
  3. Call it in _app.tsx:

import setupLocatorUI from '@locator/runtime' if (process.env.NODE_ENV === 'development') { setupLocatorUI() }

Additional information

No response

yf-hk avatar Jun 21 '23 13:06 yf-hk

I meet too

UNDERCOVERj avatar Jul 25 '23 11:07 UNDERCOVERj

I managed to fix this, by adding the ".js" at the end of the import for all files that does import JS: image

also I had to add type="module" for both package.jsons in runtime and shared folder

it seems this topic is quite known in TS community: https://stackoverflow.com/questions/62619058/appending-js-extension-on-relative-import-statements-during-typescript-compilat

The other solution I've found is by changing the babel config and tsconfig for both packages to generate the CommonJS files instead of ESM.

Willaiem avatar Oct 15 '23 01:10 Willaiem

I'm having this same issue with next.js as well.

brycefranzen avatar Nov 09 '23 16:11 brycefranzen

I fixed it to this code

const nextConfig = {
  ...
  transpilePackages: ['@locator/runtime'],
}

cyjo9603 avatar Apr 22 '24 06:04 cyjo9603