账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nodejs使用import报错
    42
    0

    nodejs使用import报错:

    导出模块:

    //D:/workspace/www/hello-world/src/data/articles.js
    
    const articles = [
        {"title": "title1","content": "content1"},
        {"title": "title2","content": "content2"},
        {"title": "title3","content": "content3"}]
    
    export default articles;

    导入模块:

    //D:/workspace/www/nodejs_scripts/test.js
    
    import articles from 'D:/workspace/www/hello-world/src/data/articles';
    
    console.log(articles);

    在命令行中运行test.js,报错:

    D:\workspace\www\nodejs_scripts
    λ node -v
    v12.11.1
    
    D:\workspace\www\nodejs_scripts
    λ node test.js
    D:\workspace\www\nodejs_scripts\test.js:3
    import articles from 'D:/workspace/www/hello-world/src/data/articles';
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at Module._compile (internal/modules/cjs/loader.js:881:18)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
        at Module.load (internal/modules/cjs/loader.js:798:32)
        at Function.Module._load (internal/modules/cjs/loader.js:711:12)
        at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
        at internal/main/run_main_module.js:17:11

    应该怎么做呢?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 先生你狠拽 普通会员 1楼

      在Node.js中,import关键字用于引入模块。但是,如果在代码中使用了import关键字,但是出现import报错,那么可能是以下原因导致的:

      1. import语法错误:检查代码中的import语句,确保它是一个有效的import语句。例如,如果你正在使用一个已存在的模块,确保你使用了正确的模块名。

      2. 模块未正确导入:确保你正确地导入了模块。例如,如果你正在导入一个名为module的模块,确保你已经定义了一个名为module的变量。

      3. 模块路径问题:如果你正在导入的模块在其他文件中,确保路径正确。例如,如果你正在导入一个名为module.js的模块,确保你在其他文件中定义了正确的位置。

      4. 代码依赖问题:如果你正在导入的模块依赖于其他模块,确保这些模块也正确地引入了。例如,如果你正在导入一个名为moduleA.js的模块,确保你在其他模块中定义了正确的位置。

      如果你在代码中多次使用import关键字,建议你检查代码的逻辑,看看是否有其他可能的错误。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部