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

Node error import assertString unexpected identifier/cannot use outside module #1581

Closed
sklawren opened this issue Jan 19, 2021 · 8 comments
Closed

Comments

@sklawren
Copy link

sklawren commented Jan 19, 2021

Describe the bug
When compiling in node (with next.js, for example) or running Jest test, Validator always throws the following error whenever anything is imported.

For example, I have a React component that imports blacklist from validator (this happens with any import though).

import blacklist from 'validator/es/lib/blacklist';

Works fine in development, but when I run jest locally, I get the following error:

{import assertString from './util/assertString';
        ^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
at Object.<anonymous> (src/utils/src/string.js:1:1)

OR in the CI for next.js (a different project, but same issue

/home/circleci/repo/node_modules/validator/es/lib/isURL.js:1
import assertString from './util/assertString';
^^^^^^
SyntaxError: Cannot use import statement outside a module

Additional context
Validator.js version: 13.5.2
Node.js version: 10.13.0 and 12.18.4
OS platform: MacOS

@sklawren sklawren changed the title Node error import assertString unexpected identifier/token Node error import assertString unexpected identifier/cannot use outside module Jan 19, 2021
@fedeci
Copy link
Contributor

fedeci commented Jan 19, 2021

Hi @sklawren, can you provide an example repo, please?

@sklawren
Copy link
Author

sklawren commented Jan 22, 2021

It's super easy to replicate. It's 100% reproducible with a brand new CRA project.

  1. Create a new Create React App npx create-react-app validator-test
  2. Go into the project, and install validator npm i -S validator

Add a file called utils.js (or whatever):

import blacklist from 'validator/es/lib/blacklist';

export const sanitize = str => blacklist(str, 'a-z');
  1. Modify the App.js file to import the sanitize function. You can copy paste this if you like.
import {sanitize} from './utils';
import './App.css';

function App() {
  const value = sanitize('a10c');
  return (
    <div className="App">
      {value}
    </div>
  );
}

export default App;
  1. npm start - Works fine. You should see '10' in the browser.
  2. npm test - Crashes.
 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import assertString from './util/assertString';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import blacklist from 'validator/es/lib/blacklist';
        | ^
      2 |
      3 | export const sanitize = str => blacklist(str, 'a-z');
      4 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (src/string.js:1:1)

@fedeci
Copy link
Contributor

fedeci commented Jan 22, 2021

Try looking at facebook/create-react-app#9938 because this is a jest "problem" and not a validator.js one :)

@sklawren
Copy link
Author

I tried that and it didn't work. What did work was changing it to:

import blacklist from 'validator/lib/blacklist';

I guess this doesn't tree-shake, but it works.

@p-iknow
Copy link

p-iknow commented Feb 12, 2021

@sklawren
i have the same issue described above, and I solved using jest.config options, transform, and transformIgnorePattern option.
if you don't use typescript, you can use babel-jest instead of ts-jest
image

@tux-tn
Copy link
Member

tux-tn commented Feb 15, 2021

Looks like the issue is related to jest and not validator . Thank you @p-iknow for providing the answer.

@newsiberian
Copy link

newsiberian commented Sep 18, 2022

I've faced w/ the same error while migrating my TS app to esm:

Node: 16.17.0

(node:85802) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

...

import assertString from './util/assertString';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at compileFunction (<anonymous>)
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1055:15)
    at Module._compile (node:internal/modules/cjs/loader:1090:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)

@JaneJeon
Copy link

I see this with vanilla ESM + TypeScript as well, as noted in #1759

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

No branches or pull requests

6 participants