Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Cannot use import statement outside a module #63

Open
mieradi opened this issue Oct 28, 2024 · 4 comments
Open

SyntaxError: Cannot use import statement outside a module #63

mieradi opened this issue Oct 28, 2024 · 4 comments

Comments

@mieradi
Copy link

mieradi commented Oct 28, 2024

Node v20
pnpm v9.12.2

I cannot use v2 of this package due to commonjs not being supported at all anymore. Is there a way we can get support for both esm and commonjs?

Thanks for the great work!

@wariomvc
Copy link

wariomvc commented Nov 1, 2024

Same problem, using NestJS
the previous version (v1.2) worked fine

@zhiweit
Copy link

zhiweit commented Nov 6, 2024

Fix: Update the config file.

You might have to make some changes to your vite config file if you are using vite.
In my case, fixed it by adding this to the app.config.ts file

import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
  vite: {
    ssr: {
      noExternal: ["pdf-img-convert", "node-fetch"],
    },
  },
});

I also experienced this error in my project using solidjs.

Cannot use import statement outside a module

/.../node_modules/.pnpm/pdf-img-convert@2.0.0_encoding@0.1.13/node_modules/pdf-img-convert/pdf-img-convert.js:1
import fetch from 'node-fetch';
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1281:20)
    at Module._compile (node:internal/modules/cjs/loader:1321:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at cjsLoader (node:internal/modules/esm/translators:348:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:297:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async nodeImport 

Answer reference from gpt 4-o

Adjust Vite Configuration
Vite needs to know how to handle certain dependencies during the build process. You can adjust the vite.config.ts file to ensure that pdf-img-convert is treated correctly.
vite.config.ts
Explanation:
The ssr.noExternal option tells Vite to bundle pdf-img-convert and node-fetch instead of treating them as external dependencies. This is crucial for libraries that use ES Module syntax.

@JCMais
Copy link

JCMais commented Nov 12, 2024

The error here is actually that this package is not setting itself as a esmodule in the package.json

The author needs to set "type": "module" in the package.json.

@gcalabria
Copy link

Setting "type": "module" in the package.json solved the bug above but now I am getting:

Failed parsing file into parts TypeError: process.getBuiltinModule is not a function

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants