itconsultis / prestashop-api-client

Javascript PrestaShop API client
MIT License
19 stars 11 forks source link

Cannot use import statement outside a module #5

Open gbelvedere opened 4 years ago

gbelvedere commented 4 years ago

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 commented 4 years ago

@gbelvedere please try:

const { rest } = require('prestashop-api-client')
gbelvedere commented 4 years ago

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