简体   繁体   中英

How to import js module into react public folder?

I have a react public folder that contains a html file and a simple js file. Then I have an src folder which is outside the public folder. When I try to import something into that js file which is outside the public folder:

import OpenIdConnect from '@luigi-project/plugin-auth-oidc';

I get the next error:

Cannot use import statement outside a module

How can I use imports into the public folder?

Normally this means your node project is not setup to use ES Modules by default and is instead using common JS modules.

Without modifying config, in files with this restriction, instead of:

import someVar from './filePath';

// ...

export { anotherVar };

use istead:

const someVar = require('./filePath');

// ...

module.exports = anotherVar;

If you wish to pursue a more consistent solution, I would advice reading though the Node.js documentation and any other documentation relevant to any library/framework/tooling that you use to find a suitable solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM