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

Cannot use import statement outside a module #5

Open
gbelvedere opened this issue Apr 20, 2020 · 2 comments
Open

Cannot use import statement outside a module #5

gbelvedere opened this issue Apr 20, 2020 · 2 comments

Comments

@gbelvedere
Copy link

Hi, I following the instructions and I get:

(node:5251) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
insert-product.js:19
import { rest } from 'prestashop-api-client';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1063:16)
at Module._compile (internal/modules/cjs/loader.js:1111:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

If I add the type module to package.json or rename the file to mjs, I get:

(node:5260) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'prestashop-api-client' imported from /var/www/vagrant/testScraper/insert-product.mjs
at packageResolve (internal/modules/esm/resolve.js:575:9)
at moduleResolve (internal/modules/esm/resolve.js:600:14)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:644:13)
at Loader.resolve (internal/modules/esm/loader.js:94:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:240:28)
at ModuleWrap. (internal/modules/esm/module_job.js:42:40)
at link (internal/modules/esm/module_job.js:41:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

package.json:

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"download-file": "^0.1.5",
"node-rest-client": "^3.1.0",
"puppeteer": "^2.1.1",
"read-json-sync": "^2.0.1",
"sync-request": "^6.1.0"
}
}

and the code is the same of the instructions:

import { rest } from 'prestashop-api-client';
const client = new rest.Client({
language: 'en',
languages: {
'en': 1,
'es': 2,
},
webservice: {
key: 'my_key',
scheme: 'http',
host: 'my_url',
root: '/api',
},
});

@rich-choy
Copy link
Contributor

rich-choy commented Apr 21, 2020

@gbelvedere please try:

const { rest } = require('prestashop-api-client')

@gbelvedere
Copy link
Author

it works now, but why? and should be changed in the docs?
thanks @rich-choy

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

2 participants