-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Comments
Hi @sklawren, can you provide an example repo, please? |
It's super easy to replicate. It's 100% reproducible with a brand new CRA project.
Add a file called utils.js (or whatever): import blacklist from 'validator/es/lib/blacklist';
export const sanitize = str => blacklist(str, 'a-z');
import {sanitize} from './utils';
import './App.css';
function App() {
const value = sanitize('a10c');
return (
<div className="App">
{value}
</div>
);
}
export default App;
|
Try looking at facebook/create-react-app#9938 because this is a |
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. |
@sklawren |
Looks like the issue is related to jest and not |
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)
|
I see this with vanilla ESM + TypeScript as well, as noted in #1759 |
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).
Works fine in development, but when I run jest locally, I get the following error:
OR in the CI for next.js (a different project, but same issue
Additional context
Validator.js version: 13.5.2
Node.js version: 10.13.0 and 12.18.4
OS platform: MacOS
The text was updated successfully, but these errors were encountered: